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

import cn.com.tenlion.systembase.dao.schoolkey.ISchoolKeyDao;
import cn.com.tenlion.systembase.pojo.bos.schoolkey.SchoolKeyBO;
import cn.com.tenlion.systembase.pojo.dtos.bigdta.SchoolKey;
import cn.com.tenlion.systembase.pojo.dtos.grid.GridDTO;
import cn.com.tenlion.systembase.pojo.dtos.school.SchoolDTO;
import cn.com.tenlion.systembase.pojo.dtos.schoolkey.SchoolKeyDTO;
import cn.com.tenlion.systembase.pojo.pos.schoolkey.SchoolKeyPO;
import cn.com.tenlion.systembase.pojo.vos.schoolkey.SchoolKeyVO;
import cn.com.tenlion.systembase.service.grid.IGridService;
import cn.com.tenlion.systembase.service.school.ISchoolService;
import cn.com.tenlion.systembase.service.schoolkey.ISchoolKeyService;
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: SchoolKeyServiceImpl
 * @Description: 学校周边重点人员
 * @Author: CodeFactory
 * @Date: 2021-10-13 10:05:21
 * @Version: 3.0
 **/
@Service
public class SchoolKeyServiceImpl extends DefaultBaseService implements ISchoolKeyService {

    @Autowired
    private ISchoolKeyDao schoolKeyDao;
    @Autowired
    private IGridService gridService;
    @Autowired
    private KafkaTemplate kafkaTemplate;
    @Autowired
    private ISchoolService schoolService;

    @Override
    public void save(SchoolKeyVO schoolKeyVO) {
        saveReturnId(schoolKeyVO);
    }

    @Override
    public void save(String token, SchoolKeyVO schoolKeyVO) {
        saveReturnId(token, schoolKeyVO);
    }

    @Override
    public String saveReturnId(SchoolKeyVO schoolKeyVO) {
        return saveReturnId(null, schoolKeyVO);
    }

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

        SchoolDTO schoolDTO = schoolService.get(schoolKeyVO.getSchoolId());
        saveKafka(schoolKeyId, schoolKeyVO, schoolDTO);
        return schoolKeyId;
    }

    @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("schoolKeyIds", ids);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        schoolKeyDao.remove(params);

        deleteKafka(idsStr);
    }

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

    @Override
    public void update(String schoolKeyId, SchoolKeyVO schoolKeyVO) {
        update(null, schoolKeyId, schoolKeyVO);
    }

    @Override
    public void update(String token, String schoolKeyId, SchoolKeyVO schoolKeyVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(schoolKeyVO);
        params.put("schoolKeyId", schoolKeyId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        schoolKeyDao.update(params);

        SchoolDTO schoolDTO = schoolService.get(schoolKeyVO.getSchoolId());
        updateKafka(schoolKeyId, schoolKeyVO, schoolDTO);
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    private SchoolKey setKafkaData(SchoolKeyVO schoolKeyVO, SchoolDTO schoolDTO) {
        SchoolKey schoolKey = new SchoolKey();
        // 学校名称
        schoolKey.setschool_name(schoolDTO.getSchoolName());
        // 姓名
        schoolKey.setfull_name(schoolKeyVO.getFullName());
        // 曾用名
        schoolKey.setname_used_before(schoolKeyVO.getNameUsedBefore());
        // 性别
        schoolKey.setgender(schoolKeyVO.getGender());
        // 危害程度名称
        schoolKey.setdegree_of_harm_name(schoolKeyVO.getDegreeOfHarmName());
        // 是否关注(是/否)
        schoolKey.setis_follow(schoolKeyVO.getIsFollow() == 0 ? "否": "是");
        // 区域全名称(/隔开)
        String locationAll = gridService.getLocationAll(schoolKeyVO.getLocationCode());
        schoolKey.setlocation_all(locationAll);
        // 区域编码
        schoolKey.setlocation_code(schoolKeyVO.getLocationCode());
        // 区域旗县区名称
        String locationArea = gridService.getLocationArea(schoolKeyVO.getLocationCode());
        schoolKey.setlocation_area(locationArea);
        // 经度
        schoolKey.setlongitude(schoolKeyVO.getLongitude());
        // 纬度
        schoolKey.setlatitude(schoolKeyVO.getLatitude());
        GridDTO gridDTO = gridService.get(schoolKeyVO.getGrid());
        if(null != gridDTO) {
            // 网格名称
            schoolKey.setgrid_name(gridDTO.getGridName());
            // 网格编码
            schoolKey.setgrid_code(gridDTO.getGridCode());
        }else {
            // 网格名称
            schoolKey.setgrid_name("暂无数据");
            // 网格编码
            schoolKey.setgrid_code("暂无数据");
        }
        return schoolKey;
    }

}