package cn.com.tenlion.systembase.service.institution.impl;

import cn.com.tenlion.systembase.pojo.dtos.bigdta.ComprehensiveRanks;
import cn.com.tenlion.systembase.pojo.dtos.grid.GridDTO;
import cn.com.tenlion.systembase.service.grid.IGridService;
import cn.com.tenlion.systembase.service.teamuser.ITeamUserService;
import com.alibaba.fastjson.JSONObject;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.exceptions.RemoveException;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.util.map.HashMapUtil;
import ink.wgink.util.UUIDUtil;
import cn.com.tenlion.systembase.dao.institution.IInstitutionDao;
import cn.com.tenlion.systembase.pojo.dtos.institution.InstitutionDTO;
import cn.com.tenlion.systembase.pojo.vos.institution.InstitutionVO;
import cn.com.tenlion.systembase.pojo.bos.institution.InstitutionBO;
import cn.com.tenlion.systembase.pojo.pos.institution.InstitutionPO;
import cn.com.tenlion.systembase.service.institution.IInstitutionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName: InstitutionServiceImpl
 * @Description: 综治机构
 * @Author: CodeFactory
 * @Date: 2021-10-11 17:24:40
 * @Version: 3.0
 **/
@Service
public class InstitutionServiceImpl extends DefaultBaseService implements IInstitutionService {

    @Autowired
    private IInstitutionDao institutionDao;
    @Autowired
    private ITeamUserService teamUserService;
    @Autowired
    private IGridService gridService;
    @Autowired
    private KafkaTemplate kafkaTemplate;

    @Override
    public void save(InstitutionVO institutionVO) {
        saveReturnId(institutionVO);
    }

    @Override
    public void save(String token, InstitutionVO institutionVO) {
        saveReturnId(token, institutionVO);
    }

    @Override
    public String saveReturnId(InstitutionVO institutionVO) {
        return saveReturnId(null, institutionVO);
    }

    @Override
    public String saveReturnId(String token, InstitutionVO institutionVO) {
        String institutionId = UUIDUtil.getUUID();
        Map<String, Object> params = HashMapUtil.beanToMap(institutionVO);
        params.put("institutionId", institutionId);
        if (StringUtils.isBlank(token)) {
            setSaveInfo(params);
        } else {
            setAppSaveInfo(token, params);
        }
        institutionDao.save(params);
        saveKafka(institutionId, institutionVO);
        return institutionId;
    }

    @Override
    public void remove(List<String> ids, String idsStr) {
        remove(null, ids, idsStr);
    }

    @Override
    public void remove(String token, List<String> ids, String idsStr) {
        checkDel(ids);
        Map<String, Object> params = getHashMap(2);
        params.put("institutionIds", ids);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        institutionDao.remove(params);

        deleteKafka(idsStr);
    }

    @Override
    public void delete(List<String> ids) {
        checkDel(ids);
        Map<String, Object> params = getHashMap(2);
        params.put("institutionIds", ids);
        institutionDao.delete(params);
    }


    public void checkDel(List<String> ids){
        for (String id : ids) {
            Integer i = teamUserService.countByInstitutionId(id);
            if(i>0){
                InstitutionDTO institutionDTO = this.get(id);
                throw new RemoveException(institutionDTO.getInstitutionName()+"机构已绑定人员，不能删除");
            }
        }

    }

    @Override
    public void update(String institutionId, InstitutionVO institutionVO) {
        update(null, institutionId, institutionVO);
    }

    @Override
    public void update(String token, String institutionId, InstitutionVO institutionVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(institutionVO);
        params.put("institutionId", institutionId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        institutionDao.update(params);

        updateKafka(institutionId, institutionVO);
    }

    @Override
    public InstitutionDTO get(Map<String, Object> params) {
        InstitutionDTO institutionDTO = institutionDao.get(params);
        if(null != institutionDTO) {
            GridDTO gridDTO = gridService.get(institutionDTO.getGrid());
            if(null != gridDTO) {
                institutionDTO.setGridName(gridDTO.getGridName());
            }
        }
        return institutionDTO;
    }

    @Override
    public InstitutionDTO get(String institutionId) {
        Map<String, Object> params = super.getHashMap(2);
        params.put("institutionId", institutionId);
        return get(params);
    }

    @Override
    public InstitutionBO getBO(Map<String, Object> params) {
        return institutionDao.getBO(params);
    }

    @Override
    public InstitutionBO getBO(String institutionId) {
        Map<String, Object> params = super.getHashMap(2);
        params.put("institutionId", institutionId);
        return getBO(params);
    }

    @Override
    public InstitutionPO getPO(Map<String, Object> params) {
        return institutionDao.getPO(params);
    }

    @Override
    public InstitutionPO getPO(String institutionId) {
        Map<String, Object> params = super.getHashMap(2);
        params.put("institutionId", institutionId);
        return getPO(params);
    }

    @Override
    public List<InstitutionDTO> list(Map<String, Object> params) {
        return institutionDao.list(params);
    }

    @Override
    public List<InstitutionBO> listBO(Map<String, Object> params) {
        return institutionDao.listBO(params);
    }

    @Override
    public List<InstitutionPO> listPO(Map<String, Object> params) {
        return institutionDao.listPO(params);
    }

    @Override
    public SuccessResultList<List<InstitutionDTO>> listPage(ListPage page) {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<InstitutionDTO> institutionDTOs = list(page.getParams());
        PageInfo<InstitutionDTO> pageInfo = new PageInfo<>(institutionDTOs);
        return new SuccessResultList<>(institutionDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
    }

    @Override
    public Integer count(Map<String, Object> params) {
        Integer count = institutionDao.count(params);
        return count == null ? 0 : count;
    }

    @Override
    public void saveKafka(String uid, InstitutionVO institutionVO) {
        ComprehensiveRanks comprehensiveRanks = setKafkaData(institutionVO);
        JSONObject jsonObject = new JSONObject();
        // 实体模板编码
        jsonObject.put("tableNumber", "C0032");
        // 数据主键
        jsonObject.put("uid", uid);
        // 设置数据操作方法
        // action: save/updete/delete
        jsonObject.put("action", "save");
        // 实体
        jsonObject.put("data", comprehensiveRanks.saveCheckToJson());
        kafkaTemplate.send("tableSync", jsonObject.toJSONString());
    }

    @Override
    public void updateKafka(String uid, InstitutionVO institutionVO) {
        ComprehensiveRanks comprehensiveRanks = setKafkaData(institutionVO);
        JSONObject jsonObject = new JSONObject();
        // 实体模板编码
        jsonObject.put("tableNumber", "C0032");
        // 数据主键
        jsonObject.put("uid", uid);
        // 设置数据操作方法
        // action: save/updete/delete
        jsonObject.put("action", "update");
        // 实体
        jsonObject.put("data", comprehensiveRanks.updateToJson());
        kafkaTemplate.send("tableSync", jsonObject.toJSONString());
    }

    @Override
    public void deleteKafka(String uid) {
        JSONObject jsonObject = new JSONObject();
        // 实体模板编码
        jsonObject.put("tableNumber", "C0032");
        // 数据主键
        jsonObject.put("uid", uid);
        // 设置数据操作方法
        // action: save/updete/delete
        jsonObject.put("action", "delete");
        // 实体
        jsonObject.put("data", new ComprehensiveRanks());
        kafkaTemplate.send("tableSync", jsonObject.toJSONString());
    }

    private ComprehensiveRanks setKafkaData(InstitutionVO institutionVO) {
        ComprehensiveRanks comprehensiveRanks = new ComprehensiveRanks();
        // 区域编码
        comprehensiveRanks.setarea_code(institutionVO.getLocationCode());
        // 区域全名称(/隔开)
        String locationAll = gridService.getLocationAll(institutionVO.getLocationCode());
        comprehensiveRanks.setarea_name_all(locationAll);
        // 区域旗县区名称
        String locationArea = gridService.getLocationArea(institutionVO.getLocationCode());
        comprehensiveRanks.setarea_name(locationArea);
        // 综治队伍名称
        comprehensiveRanks.setname(institutionVO.getInstitutionName());
        // 综治队伍类型
        comprehensiveRanks.settype(institutionVO.getInstitutionTypeText());
        // 综治队伍等级
        comprehensiveRanks.setlevel(institutionVO.getInstitutionLevelText());
        // 网格名称
        GridDTO gridDTO = gridService.get(institutionVO.getGrid());
        if(null != gridDTO) {
            // 网格名称
            comprehensiveRanks.setgrid_name(gridDTO.getGridName());
            // 网格编码
            comprehensiveRanks.setgrid_code(gridDTO.getGridCode());
        }else {
            // 网格名称
            comprehensiveRanks.setgrid_name("暂无数据");
            // 网格编码
            comprehensiveRanks.setgrid_code("暂无数据");
        }
        return comprehensiveRanks;
    }

}