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

import cn.com.tenlion.systembase.dao.keyplacepatrol.IKeyPlacePatrolDao;
import cn.com.tenlion.systembase.pojo.bos.keyplacepatrol.KeyPlacePatrolBO;
import cn.com.tenlion.systembase.pojo.dtos.bigdta.KeyPlaceCheckRenovationPatrol;
import cn.com.tenlion.systembase.pojo.dtos.keyplace.KeyPlaceDTO;
import cn.com.tenlion.systembase.pojo.dtos.keyplacepatrol.KeyPlacePatrolDTO;
import cn.com.tenlion.systembase.pojo.pos.keyplacepatrol.KeyPlacePatrolPO;
import cn.com.tenlion.systembase.pojo.vos.keyplacepatrol.KeyPlacePatrolVO;
import cn.com.tenlion.systembase.service.grid.IGridService;
import cn.com.tenlion.systembase.service.keyplace.IKeyPlaceService;
import cn.com.tenlion.systembase.service.keyplacepatrol.IKeyPlacePatrolService;
import com.alibaba.fastjson.JSONObject;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.login.oauth2.client.service.user.IUserService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.dtos.user.UserDTO;
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: KeyPlacePatrolServiceImpl
 * @Description: 重点场所巡查表
 * @Author: CodeFactory
 * @Date: 2021-11-24 10:39:11
 * @Version: 3.0
 **/
@Service
public class KeyPlacePatrolServiceImpl extends DefaultBaseService implements IKeyPlacePatrolService {

    @Autowired
    private IKeyPlacePatrolDao keyPlacePatrolDao;
    @Autowired
    private KafkaTemplate kafkaTemplate;
    @Autowired
    private IUserService userService;
    @Autowired
    private IKeyPlaceService keyPlaceService;
    @Autowired
    private IGridService gridService;

    @Override
    public void save(KeyPlacePatrolVO keyPlacePatrolVO) {
        saveReturnId(keyPlacePatrolVO);
    }

    @Override
    public void save(String token, KeyPlacePatrolVO keyPlacePatrolVO) {
        saveReturnId(token, keyPlacePatrolVO);
    }

    @Override
    public String saveReturnId(KeyPlacePatrolVO keyPlacePatrolVO) {
        return saveReturnId(null, keyPlacePatrolVO);
    }

    @Override
    public String saveReturnId(String token, KeyPlacePatrolVO keyPlacePatrolVO) {
        String keyPlacePatrolId = UUIDUtil.getUUID();
        Map<String, Object> params = HashMapUtil.beanToMap(keyPlacePatrolVO);
        params.put("keyPlacePatrolId", keyPlacePatrolId);
        if (StringUtils.isBlank(token)) {
            setSaveInfo(params);
        } else {
            setAppSaveInfo(token, params);
        }
        keyPlacePatrolDao.save(params);
        String creator = getUserData(params, keyPlacePatrolVO);
        KeyPlaceDTO keyPlaceDTO = keyPlaceService.get(keyPlacePatrolVO.getKeyPlaceId());
        saveKafka(keyPlacePatrolId, keyPlacePatrolVO, creator, keyPlaceDTO);
        return keyPlacePatrolId;
    }

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

        deleteKafka(idsStr);
    }

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

    @Override
    public void update(String keyPlacePatrolId, KeyPlacePatrolVO keyPlacePatrolVO) {
        update(null, keyPlacePatrolId, keyPlacePatrolVO);
    }

    @Override
    public void update(String token, String keyPlacePatrolId, KeyPlacePatrolVO keyPlacePatrolVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(keyPlacePatrolVO);
        params.put("keyPlacePatrolId", keyPlacePatrolId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        keyPlacePatrolDao.update(params);
        KeyPlacePatrolDTO keyPlacePatrolDTO = get(keyPlacePatrolId);
        params.put("creator", keyPlacePatrolDTO.getCreator());
        String creator = getUserData(params, keyPlacePatrolVO);
        KeyPlaceDTO keyPlaceDTO = keyPlaceService.get(keyPlacePatrolVO.getKeyPlaceId());
        updateKafka(keyPlacePatrolId, keyPlacePatrolVO, creator, keyPlaceDTO);
    }

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

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

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

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

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

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

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

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

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

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

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

    @Override
    public void saveKafka(String keyPlacePatrolId, KeyPlacePatrolVO keyPlacePatrolVO, String creatorName, KeyPlaceDTO keyPlaceDTO) {
        KeyPlaceCheckRenovationPatrol keyPlaceCheckRenovationPatrol = setKafkaData(keyPlacePatrolVO, creatorName, keyPlaceDTO);
        JSONObject jsonObject = new JSONObject();
        // 实体模板编码
        jsonObject.put("tableNumber", "C0020");
        // 数据主键
        jsonObject.put("uid", keyPlacePatrolId);
        // 设置数据操作方法
        // action: save/updete/delete
        jsonObject.put("action", "save");
        // 实体
        jsonObject.put("data", keyPlaceCheckRenovationPatrol.saveCheckToJson());
        System.out.println(keyPlaceCheckRenovationPatrol.saveCheckToJson());
        kafkaTemplate.send("tableSync", jsonObject.toJSONString());
    }

    @Override
    public void updateKafka(String keyPlacePatrolId, KeyPlacePatrolVO keyPlacePatrolVO, String creatorName, KeyPlaceDTO keyPlaceDTO) {
        KeyPlaceCheckRenovationPatrol keyPlaceCheckRenovationPatrol = setKafkaData(keyPlacePatrolVO, creatorName, keyPlaceDTO);
        JSONObject jsonObject = new JSONObject();
        // 实体模板编码
        jsonObject.put("tableNumber", "C0020");
        // 数据主键
        jsonObject.put("uid", keyPlacePatrolId);
        // 设置数据操作方法
        // action: save/updete/delete
        jsonObject.put("action", "update");
        // 实体
        jsonObject.put("data", keyPlaceCheckRenovationPatrol.updateToJson());
        System.out.println(keyPlaceCheckRenovationPatrol.updateToJson());
        kafkaTemplate.send("tableSync", jsonObject.toJSONString());
    }

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

    private KeyPlaceCheckRenovationPatrol setKafkaData(KeyPlacePatrolVO keyPlacePatrolVO, String creatorName, KeyPlaceDTO keyPlaceDTO) {
        KeyPlaceCheckRenovationPatrol keyPlaceCheckRenovationPatrol = new KeyPlaceCheckRenovationPatrol();
        keyPlaceCheckRenovationPatrol.setkey_place_checke(keyPlacePatrolVO.getKeyPlaceName());
        keyPlaceCheckRenovationPatrol.setpatrol_time(keyPlacePatrolVO.getPatrolTime());
        keyPlaceCheckRenovationPatrol.setcreator_name(creatorName);
        String locationAll = gridService.getLocationAll(keyPlaceDTO.getAreaCode());
        keyPlaceCheckRenovationPatrol.setlocation_all(locationAll);
        keyPlaceCheckRenovationPatrol.setlocation_code(keyPlaceDTO.getAreaCode());
        String locationArea = gridService.getLocationArea(keyPlaceDTO.getAreaCode());
        keyPlaceCheckRenovationPatrol.setlocation_area(locationArea);
        return keyPlaceCheckRenovationPatrol;
    }

    @Override
    public String getUserData(Map<String, Object> params, KeyPlacePatrolVO keyPlacePatrolVO) {
        String creator = params.get("creator").toString();
        UserDTO userDTO = userService.get(creator);
        if(null != userDTO) {
            creator = userDTO.getUserName();
        }
        System.out.println("keyPlaceId = " + keyPlacePatrolVO.getKeyPlaceId());
        String keyPlaceId = keyPlacePatrolVO.getKeyPlaceId();
        KeyPlaceDTO keyPlaceDTO = keyPlaceService.get(keyPlaceId);
        if(null != keyPlaceDTO) {
            keyPlacePatrolVO.setKeyPlaceName(keyPlaceDTO.getPlaceName());
        }
        return creator;
    }

}