package com.ruoyi.ibs.list.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ibs.customerselect.mapper.CustInfoBusinessMapper;
import com.ruoyi.ibs.list.domain.*;
import com.ruoyi.ibs.list.mapper.*;
import com.ruoyi.ibs.list.service.ICustInfoRecordService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.ruoyi.common.utils.SecurityUtils.*;


/**
 * 建档客户信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-19
 */
@Service
public class CustInfoRecordServiceImpl implements ICustInfoRecordService
{
    @Autowired
    private CustInfoRecordMapper custInfoRecordMapper;
    @Autowired
    private FamilyMembersRecordMapper familyMembersRecordMapper;
    @Autowired
    private RecordRelateMapper recordRelateMapper;
    @Resource
    private CustInfoBusinessMapper custInfoBusinessMapper;
    @Resource
    private CustInfoMerchantMapper custInfoMerchantMapper;
    @Resource
    private CustInfoRetailMapper custInfoRetailMapper;

    /**
     * 查询建档客户信息
     *
     * @param id 建档客户信息主键
     * @return 建档客户信息
     */
    @Override
    public CustInfoRecord selectCustInfoRecordById(Long id)
    {
        return custInfoRecordMapper.selectCustInfoRecordById(id);
    }

    @Override
    public CustInfoRecordVo selectCustInfoRecordDetailById(Long id) {
        CustInfoRecordVo custInfoRecordVo = new CustInfoRecordVo();
        CustInfoRecord custInfoRecord = selectCustInfoRecordById(id);
        custInfoRecordVo.setCustInfoRecord(custInfoRecord);
        RecordRelate recordRelate = new RecordRelate();
        if (custInfoRecord!=null){
            if (custInfoRecord.getCustType().equals("0")){
                if (custInfoRecord.getCustIdc()!=null&&!custInfoRecord.getCustIdc().equals("")){
                    FamilyMembersRecord familyMembersRecord = new FamilyMembersRecord();
                    familyMembersRecord.setRecordId(custInfoRecord.getId());
                    custInfoRecordVo.setFamilyMembersRecords(familyMembersRecordMapper.selectFamilyMembersRecordList(familyMembersRecord));

                }
            }
            recordRelate.setRecordId(custInfoRecord.getId());
            List<RecordRelate> recordRelates = recordRelateMapper.selectRecordRelateList(recordRelate);
            if (recordRelates!=null&& recordRelates.size()>0){
                custInfoRecordVo.setRecordRelates(recordRelates);
            }
        }
        return custInfoRecordVo;
    }

    /**
     * 根据客户类型和客户号/统信码反显客户信息
     *
     * @param custType
     * @param custId
     * @return
     */
    @Override
    public CustInfoRecordVo selectCustInfoByCustTypeAndCustId(String custType, String custId) {
        CustInfoRecordVo custInfoRecordVo = new CustInfoRecordVo();
        CustInfoRecord custInfoRecord = new CustInfoRecord();
        if (custType.equals("0")) {
            CustInfoRetail custInfoRetail = custInfoRetailMapper.selectCustInfoRetailByCustId("101" + custId);
            if (custInfoRetail != null){
                BeanUtils.copyProperties(custInfoRetail, custInfoRecord);
            }
        } else if (custType.equals("1")) {
            CustInfoMerchant custInfoMerchant = custInfoMerchantMapper.selectCustInfoMerchantByCustId("202" + custId);
            if (custInfoMerchant != null){
                BeanUtils.copyProperties(custInfoMerchant, custInfoRecord);
            }
        } else {
            CustInfoBusiness custInfoBusiness = custInfoBusinessMapper.selectCustInfoBusinessByCustId("202" + custId);
            if (custInfoBusiness != null){
                BeanUtils.copyProperties(custInfoBusiness, custInfoRecord);
            }
        }
        if(custInfoRecord.getId() == null){
            return custInfoRecordVo;
        }
        custInfoRecordVo.setCustInfoRecord(custInfoRecord);
        FamilyMembersRecord familyMembersRecord = new FamilyMembersRecord();
        familyMembersRecord.setRecordId(custInfoRecord.getId());
        custInfoRecordVo.setFamilyMembersRecords(familyMembersRecordMapper.selectFamilyMembersRecordList(familyMembersRecord));
        RecordRelate recordRelate = new RecordRelate();
        recordRelate.setRecordId(custInfoRecord.getId());
        custInfoRecordVo.setRecordRelates(recordRelateMapper.selectRecordRelateList(recordRelate));
        return custInfoRecordVo;
    }

    /**
     * 查询建档客户信息列表
     *
     * @param custInfoRecord 建档客户信息
     * @return 建档客户信息
     */
    @Override
    public List<CustInfoRecord> selectCustInfoRecordList(CustInfoRecord custInfoRecord)
    {
        return custInfoRecordMapper.selectCustInfoRecordList(custInfoRecord);
    }

    @Override
    public List<CustInfoRecord> selectCustInfoRecordListByCustTypeAndKeywords(String custType, String keyword) {
        String roleName = userRole();
        List<CustInfoRecord> custInfoRecords = new ArrayList<>();
        if ("head".equals(roleName)){
            custInfoRecords= custInfoRecordMapper.selectHeadCustInfoRecordListByCustTypeAndKeywords(getDeptId()/1000,custType,keyword);
        }else if ("branch".equals(roleName)){
            custInfoRecords = custInfoRecordMapper.selectCustInfoRecordListByCustTypeAndKeywords(getDeptId(),null,custType,keyword);
        }else {
            custInfoRecords = custInfoRecordMapper.selectCustInfoRecordListByCustTypeAndKeywords(null,getUsername(),custType,keyword);
        }
        return custInfoRecords;
    }

    /**
     * 新增建档客户信息
     *
     * @param custInfoRecord 建档客户信息
     * @return 结果
     */
    @Override
    public int insertCustInfoRecord(CustInfoRecord custInfoRecord)
    {
        custInfoRecord.setCreateTime(DateUtils.getNowDate());
        return custInfoRecordMapper.insertCustInfoRecord(custInfoRecord);
    }


    public int insertCustInfoRetailByRecord(CustInfoRecord custInfoRecord)
    {
        custInfoRecord.setCreateTime(DateUtils.getNowDate());
        if (custInfoRecord.getCustType().equals("0")){
            CustInfoRetail custInfoRetail = new CustInfoRetail();
            BeanUtils.copyProperties(custInfoRecord,custInfoRetail);
            custInfoRetail.setCustId("101"+custInfoRetail.getCustIdc());
            custInfoRetail.setCustType("");
            custInfoRetail.setUpdateBy(SecurityUtils.getUsername());
            return custInfoRetailMapper.insertCustInfoRetail(custInfoRetail);
        }else if(custInfoRecord.getCustType().equals("1")){
            CustInfoMerchant custInfoMerchant = new CustInfoMerchant();
            BeanUtils.copyProperties(custInfoRecord,custInfoMerchant);
            custInfoMerchant.setCustId("202"+custInfoMerchant.getSocialCreditCode());
            custInfoMerchant.setCustType("");
            custInfoMerchant.setUpdateBy(SecurityUtils.getUsername());
            return custInfoMerchantMapper.insertCustInfoMerchant(custInfoMerchant);
        }else {
            CustInfoBusiness custInfoBusiness = new CustInfoBusiness();
            BeanUtils.copyProperties(custInfoRecord,custInfoBusiness);
            custInfoBusiness.setCustId("202"+custInfoBusiness.getSocialCreditCode());
            custInfoBusiness.setCustType("");
            custInfoBusiness.setUpdateBy(SecurityUtils.getUsername());
            return custInfoBusinessMapper.insertCustInfoBusiness(custInfoBusiness);
        }

    }

    @Override
    @Transactional
    public int insertCustInfoRecordDetail(CustInfoRecord custInfoRecord, List<FamilyMembersRecord> familyMembersRecords, List<RecordRelate> recordRelates) {
        System.out.println("经过营销建档");
        CustInfoRecord record = new CustInfoRecord();
        record.setCustType(custInfoRecord.getCustType());
        Long recordId = null;
        if (custInfoRecord!=null){
            if (custInfoRecord.getCustType().equals("0")){
                record.setCustIdc(custInfoRecord.getCustIdc());
                //查询个人信息表是否存在
                    List<CustInfoRetail> custInfoRetailList = custInfoRetailMapper.selectRecord(record.getCustIdc());
                    if (custInfoRetailList!=null&&custInfoRetailList.size()>0){
                        throw new RuntimeException("当前客户身份证号已建档,无需重复建档");
                    }

                List<CustInfoRecord> custInfoRecords = custInfoRecordMapper.selectCustInfoRecordList(record);
                if (custInfoRecords!=null&&custInfoRecords.size()>0){
                    throw new RuntimeException("当前客户身份证号已建档,无需重复建档");
                }
            }
            if (custInfoRecord.getCustType().equals("1")||custInfoRecord.getCustType().equals("2")){
                record.setSocialCreditCode(custInfoRecord.getSocialCreditCode());
                //查询商户或企业信息表是否存在
                if(custInfoRecord.getCustType().equals("1")){
                    List<CustInfoMerchant> custInfoMerchantList = custInfoMerchantMapper.selectRecord(custInfoRecord.getSocialCreditCode());
                    if (custInfoMerchantList!=null&&custInfoMerchantList.size()>0){
                        throw new RuntimeException("当前商户统信码已建档,无需重复建档");
                    }
                }else{
                    List<CustInfoBusiness> custInfoBusinesses = custInfoBusinessMapper.selectRecord(custInfoRecord.getSocialCreditCode());
                    if (custInfoBusinesses!=null&&custInfoBusinesses.size()>0){
                        throw new RuntimeException("当前企业统信码已建档,无需重复建档");
                    }
                }
                List<CustInfoRecord> custInfoRecords = custInfoRecordMapper.selectCustInfoRecordList(record);
                if (custInfoRecords!=null&&custInfoRecords.size()>0){
                    throw new RuntimeException("当前商户或企业统信码已建档,无需重复建档");
                }
            }
            custInfoRecord.setUpdateTime(new Date());
            insertCustInfoRecord(custInfoRecord);
            //新客登记插入客户信息表
            insertCustInfoRetailByRecord(custInfoRecord);
            List<CustInfoRecord> records = custInfoRecordMapper.selectCustInfoRecordList(custInfoRecord);
            if (records!=null&&records.size()>0){
                recordId = records.get(0).getId();
            }
            if (custInfoRecord.getCustType().equals("0")&&familyMembersRecords.size()>0) {
                for (FamilyMembersRecord fmr:familyMembersRecords
                     ) {
                    if(fmr.getFmyName()!=null){
                    fmr.setRecordId(recordId);
                    familyMembersRecordMapper.insertFamilyMembersRecord(fmr);
                    }
                }

            }
            for (RecordRelate rr:recordRelates
                 ) {
                rr.setRecordId(recordId);
                recordRelateMapper.insertRecordRelate(rr);
            }

        }


        return 1;
    }

    /**
     * 修改建档客户信息
     *
     * @param custInfoRecord 建档客户信息
     * @return 结果
     */
    @Override
    public int updateCustInfoRecord(CustInfoRecord custInfoRecord)
    {
        custInfoRecord.setUpdateTime(DateUtils.getNowDate());
        return custInfoRecordMapper.updateCustInfoRecord(custInfoRecord);
    }

    /**
     * 批量删除建档客户信息
     *
     * @param ids 需要删除的建档客户信息主键
     * @return 结果
     */
    @Override
    public int deleteCustInfoRecordByIds(Long[] ids)
    {
        return custInfoRecordMapper.deleteCustInfoRecordByIds(ids);
    }

    /**
     * 删除建档客户信息信息
     *
     * @param id 建档客户信息主键
     * @return 结果
     */
    @Override
    public int deleteCustInfoRecordById(Long id)
    {
        return custInfoRecordMapper.deleteCustInfoRecordById(id);
    }

    @Override
    public int updateLable(CustLableDTO custLableDTO) {
        if("0".equalsIgnoreCase(custLableDTO.getCustType())){
            CustInfoRetail record = new CustInfoRetail();
            record.setCustId(custLableDTO.getCustId());
            record.setLabel(custLableDTO.getLable());
            return custInfoRetailMapper.updateCustInfoRetailLabel(record);
        }
        if("1".equalsIgnoreCase(custLableDTO.getCustType())){
            CustInfoMerchant record = new CustInfoMerchant();
            record.setCustId(custLableDTO.getCustId());
            record.setLabel(custLableDTO.getLable());
            return custInfoMerchantMapper.updateCustInfoMerchantLabel(record);
        }
        if("2".equalsIgnoreCase(custLableDTO.getCustType())){
            CustInfoBusiness record = new CustInfoBusiness();
            record.setCustId(custLableDTO.getCustId());
            record.setLabel(custLableDTO.getLable());
            return custInfoBusinessMapper.updateCustInfoBusinessLabel(record);
        }
        return 0;
    }
}
