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

import cn.com.tenlion.systembase.dao.teamuser.ITeamUserDao;
import cn.com.tenlion.systembase.pojo.bos.teamuser.TeamUserBO;
import cn.com.tenlion.systembase.pojo.dtos.bigdta.ComprehensivePerson;
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.institution.InstitutionDTO;
import cn.com.tenlion.systembase.pojo.dtos.teamuser.TeamUserDTO;
import cn.com.tenlion.systembase.pojo.pos.teamuser.TeamUserPO;
import cn.com.tenlion.systembase.pojo.vos.teamuser.TeamUserVO;
import cn.com.tenlion.systembase.service.grid.IGridService;
import cn.com.tenlion.systembase.service.institution.IInstitutionService;
import cn.com.tenlion.systembase.service.teamuser.ITeamUserService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.util.UUIDUtil;
import ink.wgink.util.map.HashMapUtil;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: TeamUserServiceImpl
 * @Description: 综治队伍
 * @Author: CodeFactory
 * @Date: 2021-10-11 17:26:41
 * @Version: 3.0
 **/
@Service
public class TeamUserServiceImpl extends DefaultBaseService implements ITeamUserService {

    @Autowired
    private ITeamUserDao teamUserDao;
    @Autowired
    private IGridService gridService;
    @Autowired
    private KafkaTemplate kafkaTemplate;
    @Autowired
    private IInstitutionService institutionService;


    public Integer countByInstitutionId(String institutionId){
        Map<String,Object> params = new HashMap<>(1);
        params.put("institutionId",institutionId);
        return this.count(params);
    }



    public void checkParams(TeamUserVO teamUserVO){

    }



    @Override
    public void save(TeamUserVO teamUserVO) {
        saveReturnId(teamUserVO);
    }

    @Override
    public void save(String token, TeamUserVO teamUserVO) {
        saveReturnId(token, teamUserVO);
    }

    @Override
    public String saveReturnId(TeamUserVO teamUserVO) {
        return saveReturnId(null, teamUserVO);
    }

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

        InstitutionDTO institutionDTO = institutionService.get(teamUserVO.getInstitutionId());
        if(null != institutionDTO) {
            teamUserVO.setGrid(institutionDTO.getGrid());
            teamUserVO.setDepartment(institutionDTO.getInstitutionName());
            teamUserVO.setLocationCode(institutionDTO.getLocationCode());
        }
        saveKafka(teamUserId, teamUserVO);
        return teamUserId;
    }

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

        deleteKafka(idsStr);
    }

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

    @Override
    public void update(String teamUserId, TeamUserVO teamUserVO) {
        update(null, teamUserId, teamUserVO);
    }

    @Override
    public void update(String token, String teamUserId, TeamUserVO teamUserVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(teamUserVO);
        params.put("teamUserId", teamUserId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        teamUserDao.update(params);

        InstitutionDTO institutionDTO = institutionService.get(teamUserVO.getInstitutionId());
        if(null != institutionDTO) {
            teamUserVO.setGrid(institutionDTO.getGrid());
            teamUserVO.setDepartment(institutionDTO.getInstitutionName());
            teamUserVO.setLocationCode(institutionDTO.getLocationCode());
        }
        updateKafka(teamUserId, teamUserVO);
    }

    @Override
    public TeamUserDTO get(Map<String, Object> params) {
        return teamUserDao.get(params);
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

    private ComprehensivePerson setKafkaData(TeamUserVO teamUserVO) {
        ComprehensivePerson comprehensivePerson = new ComprehensivePerson();
        // 区域编码
        comprehensivePerson.setarea_code(teamUserVO.getLocationCode());
        // 区域全名称(/隔开)
        String locationAll = gridService.getLocationAll(teamUserVO.getLocationCode());
        comprehensivePerson.setarea_name_all(locationAll);
        // 区域旗县区名称
        String locationArea = gridService.getLocationArea(teamUserVO.getLocationCode());
        comprehensivePerson.setarea_name(locationArea);
        // 姓名
        comprehensivePerson.setname(teamUserVO.getTeamUserName());
        // 所在队伍名称
        comprehensivePerson.setdepartment(teamUserVO.getDepartment());
        // 性别
        comprehensivePerson.setsex(teamUserVO.getTeamUserSexText());
        // 手机号
        comprehensivePerson.setphone(teamUserVO.getTeamUserPhone1());
        GridDTO gridDTO = gridService.get(teamUserVO.getGrid());
        if(null != gridDTO) {
            // 网格名称
            comprehensivePerson.setgrid_name(gridDTO.getGridName());
            // 网格编码
            comprehensivePerson.setgrid_code(gridDTO.getGridCode());
        }else {
            // 网格名称
            comprehensivePerson.setgrid_name("暂无数据");
            // 网格编码
            comprehensivePerson.setgrid_code("暂无数据");
        }
        return comprehensivePerson;
    }

}