package com.dxhy.jxpt.baseService.config.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dxhy.api.model.config.UserInfo;
import com.dxhy.api.model.config.sys.SysEnterpriseConfigEntity;
import com.dxhy.api.model.config.sys.SysGroupConfigEntity;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.jxpt.baseService.config.dao.SysEnterpriseConfigDao;
import com.dxhy.jxpt.baseService.config.dao.SysGroupConfigDao;
import com.dxhy.jxpt.baseService.config.service.LqInitService;
import com.dxhy.jxpt.baseService.config.service.SysGroupConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;


@Service("baseServicesysGroupConfigService")
@Slf4j
public class SysGroupConfigServiceImpl
		extends ServiceImpl<SysGroupConfigDao, SysGroupConfigEntity>
		implements SysGroupConfigService {

	@Resource
	private LqInitService lqInitService;

	@Resource
	private SysEnterpriseConfigDao sysEnterpriseConfigDao;
			
	@Override
	public void updateSysGroupConfigService(SysGroupConfigEntity sysGroupConfig, UserInfo userInfo) {
		DynamicContextHolder.push(DbConstant.BASICS_WRITE);
		if(ObjectUtil.isEmpty(sysGroupConfig.getId())){
			log.info("新增配置业务");
            sysGroupConfig.setCompanyName(userInfo.getCompanyName());
			sysGroupConfig.setCzrId(userInfo.getId());
			sysGroupConfig.setCzrMc(userInfo.getUsername());
			sysGroupConfig.setCreateTime(new Date());
			baseMapper.insert(sysGroupConfig);
		}else{
			log.info("修改配置业务");
			SysGroupConfigEntity oldSysGroupConfig = baseMapper.selectById(sysGroupConfig.getId());
			//原先的票种信息
			String oldPzxz = StringUtils.isBlank(oldSysGroupConfig.getPzxz())?"":oldSysGroupConfig.getPzxz();
			List<String> oldPzSplit = Arrays.asList(oldPzxz.split(","));
			List<String> oldPzList = new ArrayList<>(oldPzSplit);
			//新传进来的票种信息
			String newPzxz = sysGroupConfig.getPzxz();
			List<String> newPzSplit = Arrays.asList(newPzxz.split(","));
			List<String> newPzList = new ArrayList<>(newPzSplit);
			log.info("原先票种:{}",oldPzList);
			log.info("新修改票种:{}",newPzList);
			newPzList.removeAll(oldPzList);
			log.info("新修改票种去除原先票种后的数据:{}",newPzList);
			if(newPzList.size()>0){
				log.info("新修改的票种增加了:{}。要修改全量历史扩展表",newPzList);
				//查询所有乐企企业，做初始化处理
				List<SysEnterpriseConfigEntity> lqEnterList = sysEnterpriseConfigDao.selectList(new LambdaQueryWrapper<SysEnterpriseConfigEntity>()
						.eq(SysEnterpriseConfigEntity::getCompany, sysGroupConfig.getCompany())
						.eq(SysEnterpriseConfigEntity::getJrfs, "1")
				);
				ResponseEntity<?> responseEntity = lqInitService.saveUpdateLqFpzlDm(lqEnterList, newPzList);
				log.info("处理票种全量采集初始化返回结果:{}", JSON.toJSONString(responseEntity));
			}else{
				log.info("新修改的票种小于或和原先一样，不做处理");
			}
            DynamicContextHolder.push(DbConstant.BASICS_WRITE);
            sysGroupConfig.setCompanyName(userInfo.getCompanyName());
			sysGroupConfig.setCzrId(userInfo.getId());
			sysGroupConfig.setCzrMc(userInfo.getUsername());
			sysGroupConfig.setUpdateTime(new Date());
			baseMapper.updateById(sysGroupConfig);
		}
	}

	@Override
	public SysGroupConfigEntity getGroupInfo(String company) {
		DynamicContextHolder.push(DbConstant.BASICS_WRITE);
		SysGroupConfigEntity sysGroupConfig = baseMapper.selectOne(new LambdaQueryWrapper<SysGroupConfigEntity>().eq(SysGroupConfigEntity::getCompany, company));
		return sysGroupConfig;
	}

	@Override
	public Map<String, String> getApplyKeyByNsrsbh(String nsrsbh) {
		DynamicContextHolder.push(DbConstant.BASICS_READ);
		return baseMapper.getApplyKeyByNsrsbh(nsrsbh);
	}
}
