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

import cn.com.tenlion.systembase.dao.stuteasafe.IStuTeaSafeDao;
import cn.com.tenlion.systembase.pojo.bos.stuteasafe.StuTeaSafeBO;
import cn.com.tenlion.systembase.pojo.dtos.bigdta.StuTeaSafe;
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.stuteasafe.StuTeaSafeDTO;
import cn.com.tenlion.systembase.pojo.pos.stuteasafe.StuTeaSafePO;
import cn.com.tenlion.systembase.pojo.vos.stuteasafe.StuTeaSafeVO;
import cn.com.tenlion.systembase.service.grid.IGridService;
import cn.com.tenlion.systembase.service.school.ISchoolService;
import cn.com.tenlion.systembase.service.stuteasafe.IStuTeaSafeService;
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: StuTeaSafeServiceImpl
 * @Description: 涉及师生安全的案（事）件
 * @Author: CodeFactory
 * @Date: 2021-10-13 11:08:57
 * @Version: 3.0
 **/
@Service
public class StuTeaSafeServiceImpl extends DefaultBaseService implements IStuTeaSafeService {

    @Autowired
    private IStuTeaSafeDao stuTeaSafeDao;
    @Autowired
    private IGridService gridService;
    @Autowired
    private KafkaTemplate kafkaTemplate;
    @Autowired
    private ISchoolService schoolService;

    @Override
    public void save(StuTeaSafeVO stuTeaSafeVO) {
        saveReturnId(stuTeaSafeVO);
    }

    @Override
    public void save(String token, StuTeaSafeVO stuTeaSafeVO) {
        saveReturnId(token, stuTeaSafeVO);
    }

    @Override
    public String saveReturnId(StuTeaSafeVO stuTeaSafeVO) {
        return saveReturnId(null, stuTeaSafeVO);
    }

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

        String schoolId = stuTeaSafeVO.getSchoolId();
        SchoolDTO schoolDTO = schoolService.get(schoolId);
        if(null != schoolDTO) {
            stuTeaSafeVO.setSchoolName(schoolDTO.getSchoolName());
        }
        saveKafka(stuTeaSafeId, stuTeaSafeVO);
        return stuTeaSafeId;
    }

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

        deleteKafka(idsStr);
    }

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

    @Override
    public void update(String stuTeaSafeId, StuTeaSafeVO stuTeaSafeVO) {
        update(null, stuTeaSafeId, stuTeaSafeVO);
    }

    @Override
    public void update(String token, String stuTeaSafeId, StuTeaSafeVO stuTeaSafeVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(stuTeaSafeVO);
        params.put("stuTeaSafeId", stuTeaSafeId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        stuTeaSafeDao.update(params);

        String schoolId = stuTeaSafeVO.getSchoolId();
        SchoolDTO schoolDTO = schoolService.get(schoolId);
        if(null != schoolDTO) {
            stuTeaSafeVO.setSchoolName(schoolDTO.getSchoolName());
        }
        updateKafka(stuTeaSafeId, stuTeaSafeVO);
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    private StuTeaSafe setKafkaData(StuTeaSafeVO stuTeaSafeVO) {
        StuTeaSafe stuTeaSafe = new StuTeaSafe();
        // 学校名称
        stuTeaSafe.setschool_name(stuTeaSafeVO.getSchoolName());
        // 案(事)件编号
        stuTeaSafe.setcase_code(stuTeaSafeVO.getCaseCode());
        // 案(事)件名称
        stuTeaSafe.setcase_name(stuTeaSafeVO.getCaseName());
        // 发生日期
        stuTeaSafe.sethappen_time(stuTeaSafeVO.getHappenTime());
        // 发生地点
        stuTeaSafe.sethappen_address(stuTeaSafeVO.getHappenAddress());
        // 发生经度
        stuTeaSafe.sethappen_lng(stuTeaSafeVO.getLongitude());
        // 发生纬度
        stuTeaSafe.sethappen_lat(stuTeaSafeVO.getLatitude());
        // 案(事)件性质名称
        stuTeaSafe.setcase_nature_name(stuTeaSafeVO.getCaseNatureName());
        // 案件情况
        stuTeaSafe.setcase_situation(stuTeaSafeVO.getCaseSituation());
        // 是否破案(是/否)
        stuTeaSafe.setis_solve(stuTeaSafeVO.getCaseSolve().equals("0") ? "否": "是");
        // 作案人数
        stuTeaSafe.setcrime_count(stuTeaSafeVO.getCrimeCount());
        // 在逃人数
        stuTeaSafe.setat_large_count(stuTeaSafeVO.getAtLargeCount());
        // 抓捕人数
        stuTeaSafe.setarrest_count(stuTeaSafeVO.getArrestCount());
        // 案件侦破情况
        stuTeaSafe.setcase_solve(stuTeaSafeVO.getCaseSolve());
        // 区域全名称(/隔开)
        String locationAll = gridService.getLocationAll(stuTeaSafeVO.getHappenLocationCode());
        stuTeaSafe.setlocation_all(locationAll);
        // 区域编码
        stuTeaSafe.setlocation_code(stuTeaSafeVO.getHappenLocationCode());
        // 区域旗县区名称
        String locationArea = gridService.getLocationArea(stuTeaSafeVO.getHappenLocationCode());
        stuTeaSafe.setlocation_area(locationArea);
        // 经度
        stuTeaSafe.setlongitude(stuTeaSafeVO.getLongitude());
        // 纬度
        stuTeaSafe.setlatitude(stuTeaSafeVO.getLatitude());
        GridDTO gridDTO = gridService.get(stuTeaSafeVO.getGrid());
        if(null != gridDTO) {
            // 网格名称
            stuTeaSafe.setgrid_name(gridDTO.getGridName());
            // 网格编码
            stuTeaSafe.setgrid_code(gridDTO.getGridCode());
        }else {
            // 网格名称
            stuTeaSafe.setgrid_name("暂无数据");
            // 网格编码
            stuTeaSafe.setgrid_code("暂无数据");
        }
        return stuTeaSafe;
    }

}