package com.dk.mdm.service.ivt.outbound;

import com.dk.common.exception.BaseBusinessException;
import com.dk.common.infrastructure.constant.Constant;
import com.dk.common.infrastructure.enums.ErrorCodeEnum;
import com.dk.common.response.ResponseCodeEnum;
import com.dk.mdm.mapper.mst.CustomerMapper;
import com.dk.mdm.mapper.sale.MultiOwnerMapper;
import com.dk.mdm.model.pojo.mst.Customer;
import com.dk.mdm.model.pojo.sale.MultiOwner;
import com.dk.mdm.model.vo.ivt.OutboundVO;
import com.dk.mdm.service.common.CommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @desc   : 新建客户，业务归属共通
 * @date   : 2024/3/29 11:30
 * @author : 寇珊珊
 */
@Service
public class OutCommon {

    @Autowired
    private CommonService commonService;

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private MultiOwnerMapper multiOwnerMapper;


    /**
     * @desc   : 新建客户
     * @date   : 2024/3/27 9:28
     * @author : 寇珊珊
     */
    @Transactional(rollbackFor = {Exception.class})
    public OutboundVO insertCustomer(OutboundVO outboundVO) {
        // 如果没有客户id，要新建
        if (outboundVO.getCusId() == null) {
            List<Customer> listCustomer = customerMapper.selectByCond(new Customer().setCpId(outboundVO.getCpId()).setCusPhone(outboundVO.getCusPhone()));
            // 如果客户电话已存在
            if (listCustomer.size() > 0) {
                throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ISEXISTS_CUSPHONE.getMessage());
            }
            // 创建客户,获取编码和主键UuId
            Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.CUSTOMER.getName(), true);
            Customer customer = new Customer();
            customer.setCusId(codeMap.get("outId").toString()).setCusCode(codeMap.get("outNote").toString()).setCusName(outboundVO.getCusName())
                    .setCusPhone(outboundVO.getCusPhone()).setAddressArea(outboundVO.getAddressArea()).setAddressName(outboundVO.getAddressName())
                    .setAddressNo(outboundVO.getAddressNo()).setAddressGcj02(outboundVO.getAddressGcj02()).setAddressFull(outboundVO.getAddressFull())
                    .setContactName(outboundVO.getContactName()).setContactPhone(outboundVO.getContactPhone()).setCusFrom(outboundVO.getCusFrom())
                    .setChannelId(outboundVO.getSalesChannel()).setOrgId(outboundVO.getOrgId()).setStaffId(outboundVO.getStaffId())
                    .setReportStaff(outboundVO.getMakeStaff()).setSaleStatus(Constant.SaleStatus.CHENGJIAO.getName()).setCpId(outboundVO.getCpId());
            customerMapper.insert(customer);
            outboundVO.setCusId(customer.getCusId());
        }

        return outboundVO;
    }

    /**
     * @desc   : 新建多业务归属
     * @date   : 2024/3/27 9:33
     * @author : 寇珊珊
     */
    @Transactional(rollbackFor = {Exception.class})
    public void insertMultiOwner(OutboundVO outboundVO){
        // 业务部门业绩保存
        if (outboundVO.getOrgList() != null && outboundVO.getOrgList().size() > 0) {
            for (Map<String, Object> map : outboundVO.getOrgList()) {
                MultiOwner multiOwner = new MultiOwner();
                multiOwner.setOrderId(outboundVO.getOutId()).setOwnerId(map.get("orgId").toString())
                        .setAllocationRatio(new BigDecimal(map.get("allocationRatio").toString())).setCpId(outboundVO.getCpId());
                if (Boolean.parseBoolean(map.get("ownerFlag").toString())) {
                    multiOwner.setOwnerType(Constant.OwnerType.Z_ORG.getName());
                } else {
                    multiOwner.setOwnerType(Constant.OwnerType.C_ORG.getName());
                }
                multiOwnerMapper.insert(multiOwner);
            }
        }

        // 业务员业绩保存
        if (outboundVO.getStaffList() != null && outboundVO.getStaffList().size() > 0) {
            for (Map<String, Object> map : outboundVO.getStaffList()) {
                MultiOwner multiOwner = new MultiOwner();
                multiOwner.setOrderId(outboundVO.getOutId()).setOwnerId(map.get("staffId").toString())
                        .setAllocationRatio(new BigDecimal(map.get("allocationRatio").toString())).setCpId(outboundVO.getCpId());
                if (Boolean.parseBoolean(map.get("ownerFlag").toString())) {
                    multiOwner.setOwnerType(Constant.OwnerType.Z_STAFF.getName());
                } else {
                    multiOwner.setOwnerType(Constant.OwnerType.C_STAFF.getName());
                }
                multiOwnerMapper.insert(multiOwner);
            }
        }

    }

}
