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

import cn.com.tenlion.systembase.dao.keyareacheckrenovation.IKeyAreaCheckRenovationDao;
import cn.com.tenlion.systembase.pojo.bos.keyareacheckrenovation.KeyAreaCheckRenovationBO;
import cn.com.tenlion.systembase.pojo.dtos.bigdta.ComprehensiveCenter;
import cn.com.tenlion.systembase.pojo.dtos.bigdta.KeyArea;
import cn.com.tenlion.systembase.pojo.dtos.grid.GridDTO;
import cn.com.tenlion.systembase.pojo.dtos.keyareacheckrenovation.KeyAreaCheckRenovationDTO;
import cn.com.tenlion.systembase.pojo.pos.keyareacheckrenovation.KeyAreaCheckRenovationPO;
import cn.com.tenlion.systembase.pojo.vos.comprehensivecenter.ComprehensiveCenterVO;
import cn.com.tenlion.systembase.pojo.vos.keyareacheckrenovation.KeyAreaCheckRenovationVO;
import cn.com.tenlion.systembase.service.grid.IGridService;
import cn.com.tenlion.systembase.service.keyareacheckrenovation.IKeyAreaCheckRenovationService;
import cn.com.tenlion.systembase.util.VerifyUtil;
import com.alibaba.fastjson.JSONObject;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.util.map.HashMapUtil;
import ink.wgink.util.UUIDUtil;
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: KeyAreaCheckRenovationServiceImpl
 * @Description: 重点地区排查整治
 * @Author: CodeFactory
 * @Date: 2021-10-12 16:37:43
 * @Version: 3.0
 **/
@Service
public class KeyAreaCheckRenovationServiceImpl extends DefaultBaseService implements IKeyAreaCheckRenovationService {

    @Autowired
    private IKeyAreaCheckRenovationDao keyAreaCheckRenovationDao;
    @Autowired
    private IGridService gridService;
    @Autowired
    private KafkaTemplate kafkaTemplate;

    @Override
    public void save(KeyAreaCheckRenovationVO keyAreaCheckRenovationVO) {
        saveReturnId(keyAreaCheckRenovationVO);
    }

    @Override
    public void save(String token, KeyAreaCheckRenovationVO keyAreaCheckRenovationVO) {
        saveReturnId(token, keyAreaCheckRenovationVO);
    }

    @Override
    public String saveReturnId(KeyAreaCheckRenovationVO keyAreaCheckRenovationVO) {
        return saveReturnId(null, keyAreaCheckRenovationVO);
    }

    @Override
    public String saveReturnId(String token, KeyAreaCheckRenovationVO keyAreaCheckRenovationVO) {
        String keyAreaCheckRenovationId = UUIDUtil.getUUID();
        Map<String, Object> params = HashMapUtil.beanToMap(keyAreaCheckRenovationVO);
        params.put("keyAreaCheckRenovationId", keyAreaCheckRenovationId);
        if (StringUtils.isBlank(token)) {
            setSaveInfo(params);
        } else {
            setAppSaveInfo(token, params);
        }
        keyAreaCheckRenovationDao.save(params);

        saveKafka(keyAreaCheckRenovationId, keyAreaCheckRenovationVO);
        return keyAreaCheckRenovationId;
    }

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

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

        deleteKafka(idsStr);
    }

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

    @Override
    public void update(String keyAreaCheckRenovationId, KeyAreaCheckRenovationVO keyAreaCheckRenovationVO) {
        update(null, keyAreaCheckRenovationId, keyAreaCheckRenovationVO);
    }

    @Override
    public void update(String token, String keyAreaCheckRenovationId, KeyAreaCheckRenovationVO keyAreaCheckRenovationVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(keyAreaCheckRenovationVO);
        params.put("keyAreaCheckRenovationId", keyAreaCheckRenovationId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        keyAreaCheckRenovationDao.update(params);

        updateKafka(keyAreaCheckRenovationId, keyAreaCheckRenovationVO);
    }

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

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

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

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

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

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

    @Override
    public List<KeyAreaCheckRenovationDTO> list(Map<String, Object> params) {
        if(params.get("areaCodeLike") != null && !"".equals(params.get("areaCodeLike").toString())){
            String areaCoed = params.get("areaCodeLike").toString();
            String[] areaCodeSplit = VerifyUtil.splitStringByLength(areaCoed, 2);
            String areaCodeLike = "";
            for(int i = 5 ; i >= 0; i--){
                if(!"00".equals(areaCodeSplit[i])){
                    areaCodeLike = areaCoed.substring(0, (i + 1) * 2);
                    break;
                }
            }
            params.put("areaCodeLike",areaCodeLike);
        }
        return keyAreaCheckRenovationDao.list(params);
    }

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

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

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

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

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

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

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

    private KeyArea setKafkaData(KeyAreaCheckRenovationVO keyAreaCheckRenovationVO) {
        KeyArea keyArea = new KeyArea();
        // 治安重点地区
        keyArea.setsecurity_area(keyAreaCheckRenovationVO.getSecurityArea());
        // 治安突出问题
        keyArea.setsecurity_question_name(keyAreaCheckRenovationVO.getSecurityQuestionName());
        // 涉及区域类型
        keyArea.setinvolve_area_type_name(keyAreaCheckRenovationVO.getInvolveAreaTypeName());
        // 涉及区域范围
        keyArea.setinvolve_area_range(keyAreaCheckRenovationVO.getInvolveAreaRange());
        // 区域全名称(/隔开)
        String locationAll = gridService.getLocationAll(keyAreaCheckRenovationVO.getLocationCode());
        keyArea.setlocation_all(locationAll);
        // 区域编码
        keyArea.setlocation_code(keyAreaCheckRenovationVO.getLocationCode());
        // 区域旗县区名称
        String locationArea = gridService.getLocationArea(keyAreaCheckRenovationVO.getLocationCode());
        keyArea.setlocation_area(locationArea);
        // 经度
        keyArea.setlongitude(keyAreaCheckRenovationVO.getLongitude());
        // 纬度
        keyArea.setlatitude(keyAreaCheckRenovationVO.getLatitude());
        GridDTO gridDTO = gridService.get(keyAreaCheckRenovationVO.getGrid());
        if(null != gridDTO) {
            // 网格名称
            keyArea.setgrid_name(gridDTO.getGridName());
            // 网格编码
            keyArea.setgrid_code(gridDTO.getGridCode());
        }else {
            // 网格名称
            keyArea.setgrid_name("暂无数据");
            // 网格编码
            keyArea.setgrid_code("暂无数据");
        }
        return keyArea;
    }

}