package cn.superdesk.uniorder.dubbo.provider.organisation;

import cn.hutool.core.date.DateUtil;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.enums.common.CommonEnabledEnum;
import cn.superdesk.uniorder.dao.entity.organisation.UnifiedSupplier;
import cn.superdesk.uniorder.dao.entity.organisation.UnifiedSupplierCostConfig;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dto.param.organisation.UnifiedSupplierQueryCriteria;
import cn.superdesk.uniorder.dto.vo.unified.OrderCostUpdateParam;
import cn.superdesk.uniorder.dubbo.facade.organisation.DubboUnifiedSupplierCostConfigService;
import cn.superdesk.uniorder.service.organisation.UnifiedSupplierCostConfigService;
import cn.superdesk.uniorder.service.organisation.SysSupplierService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(version = "1.0.0", retries = -1, timeout = 10000)
public class DubboUnifiedSupplierCostConfigServiceImpl implements DubboUnifiedSupplierCostConfigService {

	@Autowired
	private UnifiedSupplierCostConfigService unifiedSupplierCostConfigService;
	@Autowired
	private SysSupplierService               unifiedSupplierService;

	@Override
	public R<List<UnifiedSupplierCostConfig>> getDataByPage(UnifiedSupplierQueryCriteria criteria, int pageNum, int pageSize) {
		return unifiedSupplierCostConfigService.getDataByPage(criteria, pageNum, pageSize);
	}

	@Override
	public Map<String, Object> addObj(UnifiedSupplierCostConfig param, LoginSysUser loginSysUser) {

		Map<String, Object> returnMap = new HashMap<>();

		// TODO 写死
		param.setEnabled(CommonEnabledEnum.启用.getKey());

		param.setCreateTime(DateUtil.date());
		param.setCreateUserId(loginSysUser.getSysUserId());

		param.setUpdateTime(DateUtil.date());
		param.setUpdateUserId(loginSysUser.getSysUserId());

		unifiedSupplierCostConfigService.insertSelective(param);
		if (param.getId() != null && param.getId() > 0) {
			//更新订单成本-本月
			UnifiedSupplier      supplier  = unifiedSupplierService.findById(param.getSaleId());
			Date                 dtnow     = DateUtil.date();
			SimpleDateFormat     sdf       = new SimpleDateFormat("yyyy-MM");
			String               dtstr     = sdf.format(dtnow);
			int                  year      = Integer.parseInt(dtstr.substring(0, 4));
			int                  month     = Integer.parseInt(dtstr.substring(5));
			OrderCostUpdateParam updateMap = new OrderCostUpdateParam();
			updateMap.setReturnPoint(param.getReturnPoint());
			updateMap.setSaleId(supplier.getSaleId());
			updateMap.setServiceNo(supplier.getServiceNo());
			updateMap.setSmonth(month);
			updateMap.setSyear(year);

			unifiedSupplierCostConfigService.updateOrderCost(updateMap);
			unifiedSupplierCostConfigService.updateOrderGoodsCost(updateMap);
		}
		returnMap.put("code", "1");
		returnMap.put("data", param);
		return returnMap;
	}

	@Override
	public Map<String, Object> updateObj(UnifiedSupplierCostConfig param, LoginSysUser loginSysUser) {

		Map<String, Object> returnMap = new HashMap<>();

		param.setUpdateTime(DateUtil.date());
		param.setUpdateUserId(loginSysUser.getSysUserId());

		unifiedSupplierCostConfigService.updateByPrimaryKeySelective(param);

		//更新订单成本-本月
		UnifiedSupplier supplier = unifiedSupplierService.findById(param.getSaleId());

		Date                 dtnow     = DateUtil.date();
		SimpleDateFormat     sdf       = new SimpleDateFormat("yyyy-MM");
		String               dtstr     = sdf.format(dtnow);
		int                  year      = Integer.parseInt(dtstr.substring(0, 4));
		int                  month     = Integer.parseInt(dtstr.substring(5));
		OrderCostUpdateParam updateMap = new OrderCostUpdateParam();
		updateMap.setReturnPoint(param.getReturnPoint());
		updateMap.setSaleId(supplier.getSaleId());
		updateMap.setServiceNo(supplier.getServiceNo());
		updateMap.setSmonth(month);
		updateMap.setSyear(year);
		unifiedSupplierCostConfigService.updateOrderCost(updateMap);
		unifiedSupplierCostConfigService.updateOrderGoodsCost(updateMap);
		returnMap.put("code", "1");
		returnMap.put("data", param);
		return returnMap;
	}

	@Override
	public void delObj(Long id) {
		unifiedSupplierCostConfigService.deleteObj(id);
	}

	@Override
	public UnifiedSupplierCostConfig findBySaleId(Long saleId) {
		return unifiedSupplierCostConfigService.findBySaleId(saleId);
	}
}
