package com.ruoyi.dimp.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.constant.ImpStatus;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.PojoUtils;
import com.ruoyi.dimp.mapper.DPctApplyInfoMapper;
import com.ruoyi.dimp.domain.DPctApplyInfo;
import com.ruoyi.dimp.domain.DPctApplyInfoPreDetail;
import com.ruoyi.dimp.domain.DPctApplyInfoPreImp;
import com.ruoyi.dimp.domain.DPctApplyInfo;
import com.ruoyi.dimp.service.IDPctApplyInfoService;

/**
 * PCT申请导入Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-19
 */
@Service
public class DPctApplyInfoServiceImpl extends ServiceImpl<DPctApplyInfoMapper, DPctApplyInfo> implements IDPctApplyInfoService {

	
	public String importData(List<DPctApplyInfoPreDetail> dataList, DPctApplyInfoPreImp imp) {

//		logger.debug("正在导入[机构积分汇总]数据:" + new Gson().toJson(data));
		long successNum = 0;
		long errorNum = 0;
		long insertNum = 0;
		long updateNum = 0;
		long skipNum = 0;
		long totalNum = dataList.size();
		StringBuilder successMsg = new StringBuilder();
		StringBuilder failureMsg = new StringBuilder();

		List<String> failMsgList = new ArrayList<String>();
//		Map<String, UaBranchInfo> branchInfoMap2 = new HashMap<String, UaBranchInfo>();
//		Map<String, AreaCache> areaInfoMap = new HashMap<String, AreaCache>();

//		Map<String, UaMemberActivityRel> activityRelMap = new HashMap<String, UaMemberActivityRel>();

		for (int j = 0; j < dataList.size(); j++) {
			DPctApplyInfoPreDetail repd = dataList.get(j);

			
			DPctApplyInfo extDPctApplyInfo = new DPctApplyInfo();
			extDPctApplyInfo.setApplyNumber(repd.getApplyNumber());
			
			int i = j + 1;
			DPctApplyInfo branchPointsSum = PojoUtils.copyTo(DPctApplyInfo.class, repd);
			List<DPctApplyInfo> list = queryList(extDPctApplyInfo);
			if (list != null && !list.isEmpty()) {
				DPctApplyInfo d = list.get(0);
//			BeanUtils.copyBeanProp(d, branchPointsSum);
				branchPointsSum.setId(d.getId());
				branchPointsSum.setUpdateTime(new Date());
				this.updateById(branchPointsSum);
				updateNum++;
				repd.setImpState(ImpStatus.IMP_UPDATE);
			} else {
				branchPointsSum.setCreateTime(new Date());
				save(branchPointsSum);

				repd.setImpState(ImpStatus.IMP_INSERT);
				insertNum++;
			}

			successNum++;

		}

		successMsg.append("导入机构积分汇总数据完成，共【" + totalNum + "】条，插入【" + insertNum + "】条");
		if (updateNum > 0) {
			successMsg.append(",更新【" + updateNum + "】条");
		}
		if (errorNum > 0) {
			successMsg.append(",失败条数【" + errorNum + "】条,");
			successMsg.append("<br/>失败原因如下:");

			for (int i = 0; i < failMsgList.size(); i++) {
				if (i < 10) {
					successMsg.append(failMsgList.get(i));
				} else {
					successMsg.append("<br/>还有更多失败，请重新导入试试");
					break;
				}
			}
		}
		imp.setTotalNum(totalNum);
		imp.setInsertNum(insertNum);
		imp.setUpdateNum(updateNum);
		imp.setSkipNum(skipNum);
		imp.setErrorNum(errorNum);
		imp.setErrReason(successMsg.toString());

		successMsg.append("<br/>");
		return successMsg.toString();

	}
	
    /**
     * 查询PCT申请导入列表
     *
     * @param dPctApplyInfo PCT申请导入
     * @return PCT申请导入集合
     * @date 2023-03-19
     */
    @Override
    public List<DPctApplyInfo> queryList(DPctApplyInfo dPctApplyInfo) {
        QueryWrapper<DPctApplyInfo> queryWrapper = Wrappers.query();
                if ( StringUtils.isNotEmpty(dPctApplyInfo.getApplyNumber())  ){
                    queryWrapper.eq("apply_number", dPctApplyInfo.getApplyNumber());
                }
                if ( StringUtils.isNotEmpty(dPctApplyInfo.getReceiptDate())  ){
                    queryWrapper.eq("receipt_date", dPctApplyInfo.getReceiptDate());
                }
                if ( StringUtils.isNotEmpty(dPctApplyInfo.getMainClassNumber())  ){
                    queryWrapper.eq("main_class_number", dPctApplyInfo.getMainClassNumber());
                }
                if ( StringUtils.isNotEmpty(dPctApplyInfo.getApplyCountry())  ){
                    queryWrapper.eq("apply_country", dPctApplyInfo.getApplyCountry());
                }
                if ( StringUtils.isNotEmpty(dPctApplyInfo.getProvinceCode())  ){
                    queryWrapper.eq("province_code", dPctApplyInfo.getProvinceCode());
                }
                if ( StringUtils.isNotEmpty(dPctApplyInfo.getProvinceName())  ){
                    queryWrapper.like("province_name", dPctApplyInfo.getProvinceName());
                }
                if ( StringUtils.isNotEmpty(dPctApplyInfo.getCityCode())  ){
                    queryWrapper.eq("city_code", dPctApplyInfo.getCityCode());
                }
                if ( StringUtils.isNotEmpty(dPctApplyInfo.getCityName())  ){
                    queryWrapper.like("city_name", dPctApplyInfo.getCityName());
                }
                if ( StringUtils.isNotEmpty(dPctApplyInfo.getApplyStreet())  ){
                    queryWrapper.eq("apply_street", dPctApplyInfo.getApplyStreet());
                }
                if ( StringUtils.isNotEmpty(dPctApplyInfo.getApplyEnglishCity())  ){
                    queryWrapper.eq("apply_english_city", dPctApplyInfo.getApplyEnglishCity());
                }
                if ( StringUtils.isNotEmpty(dPctApplyInfo.getApplyEnglishStreet())  ){
                    queryWrapper.eq("apply_english_street", dPctApplyInfo.getApplyEnglishStreet());
                }
                if ( StringUtils.isNotEmpty(dPctApplyInfo.getInternApplyDay())  ){
                    queryWrapper.eq("intern_apply_day", dPctApplyInfo.getInternApplyDay());
                }
                if ( StringUtils.isNotEmpty(dPctApplyInfo.getIsPay())  ){
                    queryWrapper.eq("is_pay", dPctApplyInfo.getIsPay());
                }
                if (  StringUtils.isNotNull(dPctApplyInfo.getStatus())){
                    queryWrapper.eq("status", dPctApplyInfo.getStatus());
                }
        queryWrapper.orderByDesc("id");
        return this.list(queryWrapper);
    }
}
