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

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ibs.list.domain.*;
import com.ruoyi.ibs.list.mapper.SysGroupCustomerMapper;
import com.ruoyi.ibs.list.mapper.SysGroupMapper;
import com.ruoyi.ibs.list.service.ISysGroupCustomerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import static com.ruoyi.ibs.enumertion.CustTypeEnum.*;

/**
 * 分群客户关联关系,导入的客群用户存放在这里Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-14
 */
@Service
public class SysGroupCustomerServiceImpl implements ISysGroupCustomerService {
    @Autowired
    private SysGroupCustomerMapper sysGroupCustomerMapper;
    @Autowired
    private SysGroupMapper sysGroupMapper;

    /**
     * 查询分群客户关联关系,导入的客群用户存放在这里
     *
     * @param id 分群客户关联关系,导入的客群用户存放在这里主键
     * @return 分群客户关联关系, 导入的客群用户存放在这里
     */
    @Override
    public SysGroupCustomer selectSysGroupCustomerById(Long id) {
        return sysGroupCustomerMapper.selectSysGroupCustomerById(id);
    }

    /**
     * 查询分群客户关联关系,导入的客群用户存放在这里列表
     *
     * @param sysGroupCustomer 分群客户关联关系,导入的客群用户存放在这里
     * @return 分群客户关联关系, 导入的客群用户存放在这里
     */
    @Override
    public List<SysGroupCustomer> selectSysGroupCustomerList(SysGroupCustomer sysGroupCustomer) {
        return sysGroupCustomerMapper.selectSysGroupCustomerList(sysGroupCustomer);
    }

    @Override
    public void selectSysGroupCustomerListExport(HttpServletResponse response, String groupId,String pushStatus) {
        SysGroup group = sysGroupMapper.selectSysGroupByGroupId(groupId);
        List<SysGroupCustomerExportVo> customerExportVos = new ArrayList<>();
        List<SysGroupMerchantExportVo> merchantExportVos = new ArrayList<>();
        List<SysGroupBusinessExportVo> businessExportVos = new ArrayList<>();
        List<CustomerList>  list = sysGroupCustomerMapper.selectSysGroupCustomerListExport(groupId,pushStatus);
        convertExport(response,list,customerExportVos,merchantExportVos,businessExportVos,group.getCustType());

    }

    @Override
    public void selectSysGroupCustomerListDetailExport(HttpServletResponse response, String groupId) {
        SysGroup group = sysGroupMapper.selectSysGroupByGroupId(groupId);
        List<SysGroupCustomerExportVo> customerExportVos = new ArrayList<>();
        List<SysGroupMerchantExportVo> merchantExportVos = new ArrayList<>();
        List<SysGroupBusinessExportVo> businessExportVos = new ArrayList<>();
        List<CustomerList>  list = sysGroupMapper.selectCustomerList(groupId,null,null,null,null,null,null,null,null,null,null,null,null);
        convertExport(response,list,customerExportVos,merchantExportVos,businessExportVos,group.getCustType());
    }

    @Override
    public void convertExport(HttpServletResponse response,List<CustomerList> list,List<SysGroupCustomerExportVo> customerExportVos,List<SysGroupMerchantExportVo> merchantExportVos,List<SysGroupBusinessExportVo> businessExportVos,String groupType){
        list.stream().forEach(l->{
            if (l.getPushStatus().equals("0")){
                l.setPushStatus("未推送");
            }else if (l.getPushStatus().equals("1")){
                l.setPushStatus("已推送");
            }else if (l.getPushStatus().equals("2")){
                l.setPushStatus("推送失败");
            }
            else if (l.getPushStatus().equals("3")){
                l.setPushStatus("无法推送");
            }
            if (Objects.nonNull(l.getVisitStatus())){
                if(l.getVisitStatus().equals("1")){
                    l.setVisitStatus("已走访");
                } else if (l.getVisitStatus().equals("0")){
                    l.setVisitStatus("未走访");
                }
            }
        });
        if (PERSONAL.getCode().equals(groupType)){
            customerExportVos = list.stream().map(l->{
                SysGroupCustomerExportVo customerExportVo = new SysGroupCustomerExportVo();
                BeanUtils.copyProperties(l,customerExportVo);
                customerExportVo.setCustType(PERSONAL.getName());
                return customerExportVo;
            }).collect(Collectors.toList());
            ExcelUtil<SysGroupCustomerExportVo> util = new ExcelUtil<SysGroupCustomerExportVo>(SysGroupCustomerExportVo.class);
            util.exportExcel(response, customerExportVos, "客群圈选导出分群数据");

        } else if (MERCHANT.getCode().equals(groupType)) {
             merchantExportVos = list.stream().map(l->{
                SysGroupMerchantExportVo merchantExportVo = new SysGroupMerchantExportVo();
                BeanUtils.copyProperties(l,merchantExportVo);
                merchantExportVo.setCustType(MERCHANT.getName());
                return merchantExportVo;
            }).collect(Collectors.toList());
            ExcelUtil<SysGroupMerchantExportVo> util = new ExcelUtil<SysGroupMerchantExportVo>(SysGroupMerchantExportVo.class);
            util.exportExcel(response, merchantExportVos, "客群圈选导出分群数据");
        } else if (ENTERPRISE.getCode().equals(groupType)) {
            businessExportVos = list.stream().map(l->{
                SysGroupBusinessExportVo businessExportVo = new SysGroupBusinessExportVo();
                BeanUtils.copyProperties(l,businessExportVo);
                businessExportVo.setCustType(ENTERPRISE.getName());
                return businessExportVo;
            }).collect(Collectors.toList());
            ExcelUtil<SysGroupBusinessExportVo> util = new ExcelUtil<SysGroupBusinessExportVo>(SysGroupBusinessExportVo.class);
            util.exportExcel(response, businessExportVos, "客群圈选导出分群数据");
        }
    }

    @Override
    public void convertExport1(HttpServletResponse response,List<CustomerList> list,List<SysGroupCustomerExportVo> customerExportVos,
                               List<SysGroupMerchantExportVo> merchantExportVos,List<SysGroupBusinessExportVo> businessExportVos,String groupType, String exportType){
        list.stream().forEach(l->{
            if (l.getPushStatus().equals("0")){
                l.setPushStatus("未推送");
            }else if (l.getPushStatus().equals("1")){
                l.setPushStatus("已推送");
            }else if (l.getPushStatus().equals("2")){
                l.setPushStatus("推送失败");
            }
            else if (l.getPushStatus().equals("3")){
                l.setPushStatus("无法推送");
            }
        });
        String[] exportTypes = exportType.split(",");
        List<List<Map<String, Object>>> filterList = new ArrayList<>();
        if (PERSONAL.getCode().equals(groupType)){
            for (CustomerList cust : list){
                List<Map<String, Object>> perList = new ArrayList<>();
                Map<String, Object> map = new HashMap<>();
                for (String re : exportTypes){
                    switch(re) {
                        case "custName": perList.add(new HashMap(){
                            {
                                put("客户名称", cust.getCustName());
                            }
                        }); break;
                        case "idType": perList.add(new HashMap(){
                            {
                                put("证件类型", cust.getIdType());
                            }
                        }); break;
                        case "custIdc": perList.add(new HashMap(){
                            {
                                put("证件号", cust.getCustIdc());
                            }
                        }); break;
                        case "custPhone": perList.add(new HashMap(){
                            {
                                put("联系方式", cust.getCustPhone());
                            }
                        }); break;
                        case "custId": perList.add(new HashMap(){
                            {
                                put("客户号", cust.getCustId());
                            }
                        }); break;
                        case "branchName": perList.add(new HashMap(){
                            {
                                put("推送支行", cust.getBranchName());
                            }
                        }); break;
                        case "outlets": perList.add(new HashMap(){
                            {
                                put("推送支行网点", cust.getOutlets());
                            }
                        }); break;
                        case "tellerNameWithId": perList.add(new HashMap(){
                            {
                                put("柜员ID", cust.getTellerId());
                            }
                        }); break;
                        case "custLb": perList.add(new HashMap(){
                            {
                                put("是否行内客户", cust.getCustLb());
                            }
                        }); break;
                        case "pushStatus": perList.add(new HashMap(){
                            {
                                put("导入状态", cust.getPushStatus());
                            }
                        }); break;
                        case "createTime":perList.add(new HashMap(){
                            {
                                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                                String createTimeStr = sdf.format(cust.getCreateTime());
                                put("导入时间", createTimeStr);
                            }
                        }); break;
                    }
                }
                filterList.add(perList);
            }
        }else if (ENTERPRISE.getCode().equals(groupType)) {
            for (CustomerList cust : list){
                List<Map<String, Object>> entList = new ArrayList<>();
                Map<String, Object> map = new HashMap<>();
                for (String re : exportTypes){
                    switch(re) {
                        case "custName": entList.add(new HashMap(){
                            {
                                put("企业名称", cust.getCustName());
                            }
                        }); break;
                        case "socialCreditCodeType":entList.add(new HashMap(){
                            {
                                put("企业证件类型", cust.getSocialCreditCodeType());
                            }
                        }); break;
                        case "socialCreditCode": entList.add(new HashMap(){
                            {
                                put("统一社会信用代码", cust.getSocialCreditCode());
                            }
                        }); break;
                        case "idType": entList.add(new HashMap(){
                            {
                                put("法人证件类型", cust.getIdType());
                            }
                        }); break;
                        case "custIdc": entList.add(new HashMap(){
                            {
                                put("证件号", cust.getCustIdc());
                            }
                        }); break;
                        case "lpName": entList.add(new HashMap(){
                            {
                                put("法人姓名", cust.getLpName());
                            }
                        }); break;
                        case "custPhone": entList.add(new HashMap(){
                            {
                                put("联系方式", cust.getCustPhone());
                            }
                        }); break;
                        case "custId": entList.add(new HashMap(){
                            {
                                put("客户号", cust.getCustId());
                            }
                        }); break;
                        case "pushStatus": entList.add(new HashMap(){
                            {
                                put("导入状态", cust.getPushStatus());
                            }
                        }); break;
                        case "branchName": entList.add(new HashMap(){
                            {
                                put("推送支行", cust.getBranchName());
                            }
                        }); break;
                        case "outlets": entList.add(new HashMap(){
                            {
                                put("推送支行网点", cust.getOutlets());
                            }
                        }); break;
                        case "tellerNameWithId": entList.add(new HashMap(){
                            {
                                put("柜员ID", cust.getTellerNameWithId());
                            }
                        }); break;
                        case "custLb": entList.add(new HashMap(){
                            {
                                put("是否行内客户", cust.getCustLb());
                            }
                        }); break;

                        case "createTime":entList.add(new HashMap(){
                            {
                                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                                String createTimeStr = sdf.format(cust.getCreateTime());
                                put("导入时间", createTimeStr);
                            }
                        }); break;

                    }
                }

                filterList.add(entList);
            }
        }else if (MERCHANT.getCode().equals(groupType)) {
            for (CustomerList cust : list){
                List<Map<String, Object>> merList = new ArrayList<>();
                Map<String, Object> map = new HashMap<>();
                for (String re : exportTypes){
                    switch(re) {
                        case "custName": merList.add(new HashMap(){
                            {
                                put("商户名称", cust.getCustName());
                            }
                        }); break;
                        case "socialCreditCodeType": merList.add(new HashMap(){
                            {
                                put("经营者证件类型", cust.getSocialCreditCodeType());
                            }
                        }); break;
                        case "socialCreditCode": merList.add(new HashMap(){
                            {
                                put("统一社会信用代码", cust.getSocialCreditCode());
                            }
                        }); break;
                        case "idType": merList.add(new HashMap(){
                            {
                                put("经营者证件类型", cust.getIdType());
                            }
                        }); break;
                        case "custIdc": merList.add(new HashMap(){
                            {
                                put("证件号", cust.getCustIdc());
                            }
                        }); break;
                        case "lpName": merList.add(new HashMap(){
                            {
                                put("经营者姓名", cust.getLpName());
                            }
                        }); break;
                        case "custPhone": merList.add(new HashMap(){
                            {
                                put("联系方式", cust.getCustPhone());
                            }
                        }); break;
                        case "custId": merList.add(new HashMap(){
                            {
                                put("客户号", cust.getCustId());
                            }
                        }); break;
                        case "branchName": merList.add(new HashMap(){
                            {
                                put("推送支行", cust.getBranchName());
                            }
                        }); break;
                        case "outlets": merList.add(new HashMap(){
                            {
                                put("推送支行网点", cust.getOutlets());
                            }
                        }); break;
                        case "tellerNameWithId": merList.add(new HashMap(){
                            {
                                put("柜员ID", cust.getTellerNameWithId());
                            }
                        }); break;
                        case "custLb": merList.add(new HashMap(){
                            {
                                put("是否行内客户", cust.getCustLb());
                            }
                        }); break;
                        case "pushStatus": merList.add(new HashMap(){
                            {
                                put("导入状态", cust.getPushStatus());
                            }
                        }); break;
                        case "createTime":merList.add(new HashMap(){
                            {
                                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                                String createTimeStr = sdf.format(cust.getCreateTime());
                                put("导入时间", createTimeStr);
                            }
                        }); break;

                    }
                }
                filterList.add(merList);
            }
        }

        if (PERSONAL.getCode().equals(groupType)){
            customerExportVos = list.stream().map(l->{
                SysGroupCustomerExportVo customerExportVo = new SysGroupCustomerExportVo();
                BeanUtils.copyProperties(l,customerExportVo);
                customerExportVo.setCustType(PERSONAL.getName());
                return customerExportVo;
            }).collect(Collectors.toList());
            try {
                ExcelUtil.exportExcelByColumn(response, filterList, "客群圈选导出分群数据");
            }catch(IOException e){
                e.printStackTrace();
            }
//            ExcelUtil<SysGroupCustomerExportVo> util = new ExcelUtil<SysGroupCustomerExportVo>(SysGroupCustomerExportVo.class);
//            util.exportExcel(response, customerExportVos, "客群圈选导出分群数据");

        } else if (MERCHANT.getCode().equals(groupType)) {
            merchantExportVos = list.stream().map(l->{
                SysGroupMerchantExportVo merchantExportVo = new SysGroupMerchantExportVo();
                BeanUtils.copyProperties(l,merchantExportVo);
                merchantExportVo.setCustType(MERCHANT.getName());
                return merchantExportVo;
            }).collect(Collectors.toList());
            try {
                ExcelUtil.exportExcelByColumn(response, filterList, "客群圈选导出分群数据");
            }catch(IOException e){
                e.printStackTrace();
            }
//            ExcelUtil<SysGroupMerchantExportVo> util = new ExcelUtil<SysGroupMerchantExportVo>(SysGroupMerchantExportVo.class);
//            util.exportExcel(response, merchantExportVos, "客群圈选导出分群数据");
        } else if (ENTERPRISE.getCode().equals(groupType)) {
            businessExportVos = list.stream().map(l->{
                SysGroupBusinessExportVo businessExportVo = new SysGroupBusinessExportVo();
                BeanUtils.copyProperties(l,businessExportVo);
                businessExportVo.setCustType(ENTERPRISE.getName());
                return businessExportVo;
            }).collect(Collectors.toList());
            try {
                ExcelUtil.exportExcelByColumn(response, filterList, "客群圈选导出分群数据");
            }catch(IOException e){
                e.printStackTrace();
            }
//            ExcelUtil<SysGroupBusinessExportVo> util = new ExcelUtil<SysGroupBusinessExportVo>(SysGroupBusinessExportVo.class);
//            util.exportExcel(response, businessExportVos, "客群圈选导出分群数据");
        }
    }

    @Override
    public void selectSysGroupCustomerListExportByCustIds(HttpServletResponse response,String groupId, List<String> custIds,String exportType) {
        List<CustomerList> list = sysGroupCustomerMapper.selectSysGroupCustomerListExportByCustIds(groupId,custIds);
        SysGroup group = sysGroupMapper.selectSysGroupByGroupId(groupId);
        List<SysGroupCustomerExportVo> customerExportVos = new ArrayList<>();
        List<SysGroupMerchantExportVo> merchantExportVos = new ArrayList<>();
        List<SysGroupBusinessExportVo> businessExportVos = new ArrayList<>();
//        convertExport(response,list,customerExportVos,merchantExportVos,businessExportVos,group.getCustType());
        convertExport1(response,list,customerExportVos,merchantExportVos,businessExportVos,group.getCustType(),exportType);
    }

    /**
     * 新增分群客户关联关系,导入的客群用户存放在这里
     *
     * @param sysGroupCustomer 分群客户关联关系,导入的客群用户存放在这里
     * @return 结果
     */
    @Override
    public int insertSysGroupCustomer(SysGroupCustomer sysGroupCustomer) {
        sysGroupCustomer.setCreateTime(DateUtils.getNowDate());
        return sysGroupCustomerMapper.insertSysGroupCustomer(sysGroupCustomer);
    }

    /**
     * 修改分群客户关联关系,导入的客群用户存放在这里
     *
     * @param sysGroupCustomer 分群客户关联关系,导入的客群用户存放在这里
     * @return 结果
     */
    @Override
    public int updateSysGroupCustomer(SysGroupCustomer sysGroupCustomer) {
        sysGroupCustomer.setUpdateTime(DateUtils.getNowDate());
        return sysGroupCustomerMapper.updateSysGroupCustomer(sysGroupCustomer);
    }

    /**
     * 批量删除分群客户关联关系,导入的客群用户存放在这里
     *
     * @param ids 需要删除的分群客户关联关系,导入的客群用户存放在这里主键
     * @return 结果
     */
    @Override
    public int deleteSysGroupCustomerByIds(Long[] ids) {
        return sysGroupCustomerMapper.deleteSysGroupCustomerByIds(ids);
    }

    /**
     * 删除分群客户关联关系,导入的客群用户存放在这里信息
     *
     * @param id 分群客户关联关系,导入的客群用户存放在这里主键
     * @return 结果
     */
    @Override
    public int deleteSysGroupCustomerById(Long id) {
        return sysGroupCustomerMapper.deleteSysGroupCustomerById(id);
    }

    @Override
    public int updateOutletsIdAndUserIdByUsername(String groupId) {
        return sysGroupCustomerMapper.updateOutletsIdAndUserIdByUsername(groupId);
    }

    @Override
    public int updateCustIdAndCustIsn(String groupId,String custType,List<String> custIdcs) {
        return sysGroupCustomerMapper.updateCustIdAndCustIsn(groupId,custType,custIdcs);
    }

    @Override
    public int updateCustIdAndCustIsnBySocialCreditCode(String groupId, String custType, List<String> socialCreditCodes) {
        return sysGroupCustomerMapper.updateCustIdAndCustIsnBySocialCreditCode(groupId,custType,socialCreditCodes);
    }

    @Override
    public int insertSysGroupCustomerBatch(List<SysGroupCustomer> sysGroupCustomers) {
        return sysGroupCustomerMapper.insertSysGroupCustomerBatch(sysGroupCustomers);
    }

    @Override
    public String selectCustIdcBySocialCreditCode(String custType,String socialCreditCode) {
        return sysGroupCustomerMapper.selectCustIdcBySocialCreditCode(custType,socialCreditCode);
    }
}
