package com.ensign.crm.module.crm.service;

import com.ensign.crm.framework.common.enums.CommonStatusEnum;
import com.ensign.crm.framework.excel.core.util.ExcelUtils;
import com.ensign.crm.module.crm.controller.crm.vs.GenerateTemplateReqVo;
import com.ensign.crm.module.crm.controller.crm.vs.RegionOptionItem;
import com.ensign.crm.module.crm.model.DealerRegistrationModel;
import com.ensign.crm.module.system.controller.admin.dict.vo.data.DictDataSaveReqVO;
import com.ensign.crm.module.system.dal.dataobject.dict.DictDataDO;
import com.ensign.crm.module.system.enums.DictTypeConstants;
import com.ensign.crm.module.system.service.dict.DictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: TODO
 * @Date: 29/11/2024 2:29 pm
 * @Created: by ZZSLL
 */

@Service
public class ExcelTemplateService {

    @Autowired
    private DictDataService dictDataService;

    /**
     * 经销商登记
     *
     * @param data
     * @param response
     */
    public void generateDealerRegistration(GenerateTemplateReqVo data, HttpServletResponse response) throws IOException {
        List<RegionOptionItem> region = data.getRegion();
        Map<String, String> regionMap = region.stream().collect(Collectors.toMap(RegionOptionItem::getValue, RegionOptionItem::getLabel));
        List<DictDataDO> existData = dictDataService.getDictDataList(CommonStatusEnum.ENABLE.getStatus(), DictTypeConstants.REGION_OPTION);
        List<DictDataDO> update = new ArrayList<>();
        List<RegionOptionItem> insert = new ArrayList<>();
        for (DictDataDO item : existData) {
            String name = item.getLabel();
            String value = item.getValue();
            if (regionMap.containsKey(value)) {
                if (!name.equals(regionMap.get(name))) {
                    item.setLabel(regionMap.get(value));
                    update.add(item);
                }
            }
        }

        Map<String, DictDataDO> existMap = existData.stream().collect(Collectors.toMap(DictDataDO::getValue, d -> d));
        List<DictDataDO> delete = existMap.entrySet().stream()
                .filter(entry -> !regionMap.containsKey(entry.getKey()))
                .map(Map.Entry::getValue)
                .collect(Collectors.toList());

        for (Map.Entry<String, String> entry : regionMap.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();

            if (!existMap.containsKey(key)) {
                insert.add(new RegionOptionItem(value, key));
            }
        }

        dictDataService.updateById(update);
        for (RegionOptionItem item : insert) {
            dictDataService.createDictData(new DictDataSaveReqVO(null, 0, item.getLabel(), item.getValue(), DictTypeConstants.REGION_OPTION, CommonStatusEnum.ENABLE.getStatus(), null, null, null));
        }
        dictDataService.deleteById(delete.stream().map(DictDataDO::getId).collect(Collectors.toSet()));
        ExcelUtils.write(response, "经销商登记引出模板.xlsx", "Sheet1", DealerRegistrationModel.class, data.getData());
    }

    public List<DealerRegistrationModel> parsingExcel(MultipartFile file) throws IOException {
        return ExcelUtils.read(file, DealerRegistrationModel.class);
    }
}
