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

import cn.com.tenlion.systembase.dao.school.ISchoolDao;
import cn.com.tenlion.systembase.pojo.bos.school.SchoolBO;
import cn.com.tenlion.systembase.pojo.dtos.bigdta.School;
import cn.com.tenlion.systembase.pojo.dtos.grid.GridDTO;
import cn.com.tenlion.systembase.pojo.dtos.school.SchoolDTO;
import cn.com.tenlion.systembase.pojo.pos.school.SchoolPO;
import cn.com.tenlion.systembase.pojo.vos.school.SchoolVO;
import cn.com.tenlion.systembase.service.grid.IGridService;
import cn.com.tenlion.systembase.service.school.ISchoolService;
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: SchoolServiceImpl
 * @Description: 学校
 * @Author: CodeFactory
 * @Date: 2021-10-12 17:11:37
 * @Version: 3.0
 **/
@Service
public class SchoolServiceImpl extends DefaultBaseService implements ISchoolService {

    @Autowired
    private ISchoolDao schoolDao;
    @Autowired
    private IGridService gridService;
    @Autowired
    private KafkaTemplate kafkaTemplate;

    @Override
    public void save(SchoolVO schoolVO) {
        saveReturnId(schoolVO);
    }

    @Override
    public void save(String token, SchoolVO schoolVO) {
        saveReturnId(token, schoolVO);
    }

    @Override
    public String saveReturnId(SchoolVO schoolVO) {
        return saveReturnId(null, schoolVO);
    }

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

        saveKafka(schoolId, schoolVO);
        return schoolId;
    }

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

        deleteKafka(idsStr);
    }

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

    @Override
    public void update(String schoolId, SchoolVO schoolVO) {
        update(null, schoolId, schoolVO);
    }

    @Override
    public void update(String token, String schoolId, SchoolVO schoolVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(schoolVO);
        params.put("schoolId", schoolId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        schoolDao.update(params);

        updateKafka(schoolId, schoolVO);
    }

    @Override
    public SchoolDTO get(Map<String, Object> params) {
        SchoolDTO schoolDTO = schoolDao.get(params);
        if(null != schoolDTO && !"1".equals(schoolDTO.getDataFrom())) {
            GridDTO gridDTO = gridService.get(schoolDTO.getGrid());
            if(null != gridDTO) {
                schoolDTO.setGridName(gridDTO.getGridName());
            }
        }
        return schoolDTO;
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

    private School setKafkaData(SchoolVO schoolVO) {
        School school = new School();
        // 学校名称
        school.setschool_name(schoolVO.getSchoolName());
        // 学校地址
        school.setschool_address(schoolVO.getSchoolAddress());
        // 学校办学类型
        school.setrunning_type_name(schoolVO.getRunningTypeName());
        // 区域全名称(/隔开)
        String locationAll = gridService.getLocationAll(schoolVO.getLocationCode());
        school.setlocation_all(locationAll);
        // 区域编码
        school.setlocation_code(schoolVO.getLocationCode());
        // 区域旗县区名称
        String locationArea = gridService.getLocationArea(schoolVO.getLocationCode());
        school.setlocation_area(locationArea);
        // 经度
        if(null != schoolVO.getLongitude()) {
            school.setlongitude(schoolVO.getLongitude().toString());
        }else {
            school.setlongitude("暂无数据");
        }
        // 纬度
        if(null != schoolVO.getLatitude()) {
            school.setlatitude(schoolVO.getLatitude().toString());
        }else {
            school.setlatitude("暂无数据");
        }

        if(null != schoolVO.getGrid()) {
            GridDTO gridDTO = gridService.get(schoolVO.getGrid());
            if(null != gridDTO) {
                // 网格名称
                school.setgrid_name(gridDTO.getGridName());
                // 网格编码
                school.setgrid_code(gridDTO.getGridCode());
            }else {
                // 网格名称
                school.setgrid_name("暂无数据");
                // 网格编码
                school.setgrid_code("暂无数据");
            }
        }else {
            // 网格名称
            school.setgrid_name("暂无数据");
            // 网格编码
            school.setgrid_code("暂无数据");
        }

        return school;
    }

}