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

import cn.com.tenlion.systembase.dao.keyareacheckrenovationpatrol.IKeyAreaCheckRenovationPatrolDao;
import cn.com.tenlion.systembase.pojo.bos.keyareacheckrenovationpatrol.KeyAreaCheckRenovationPatrolBO;
import cn.com.tenlion.systembase.pojo.dtos.bigdta.KeyAreaCheckRenovationPatrol;
import cn.com.tenlion.systembase.pojo.dtos.keyareacheckrenovation.KeyAreaCheckRenovationDTO;
import cn.com.tenlion.systembase.pojo.dtos.keyareacheckrenovationpatrol.KeyAreaCheckRenovationPatrolDTO;
import cn.com.tenlion.systembase.pojo.pos.keyareacheckrenovationpatrol.KeyAreaCheckRenovationPatrolPO;
import cn.com.tenlion.systembase.pojo.vos.keyareacheckrenovationpatrol.KeyAreaCheckRenovationPatrolVO;
import cn.com.tenlion.systembase.service.grid.IGridService;
import cn.com.tenlion.systembase.service.keyareacheckrenovation.IKeyAreaCheckRenovationService;
import cn.com.tenlion.systembase.service.keyareacheckrenovationpatrol.IKeyAreaCheckRenovationPatrolService;
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.date.DateUtil;
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.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName: KeyAreaCheckRenovationPatrolServiceImpl
 * @Description: 重点区域巡查记录
 * @Author: CodeFactory
 * @Date: 2021-10-20 09:50:11
 * @Version: 3.0
 **/
@Service
public class KeyAreaCheckRenovationPatrolServiceImpl extends DefaultBaseService implements IKeyAreaCheckRenovationPatrolService {

    @Autowired
    private IKeyAreaCheckRenovationPatrolDao keyAreaCheckRenovationPatrolDao;
    @Autowired
    private IKeyAreaCheckRenovationService keyAreaCheckRenovationService;
    @Autowired
    private KafkaTemplate kafkaTemplate;
    @Autowired
    private IUserService userService;
    @Autowired
    private IGridService gridService;

    @Override
    public void save(KeyAreaCheckRenovationPatrolVO keyAreaCheckRenovationPatrolVO) {
        saveReturnId(keyAreaCheckRenovationPatrolVO);
    }

    @Override
    public void save(String token, KeyAreaCheckRenovationPatrolVO keyAreaCheckRenovationPatrolVO) {
        saveReturnId(token, keyAreaCheckRenovationPatrolVO);
    }

    @Override
    public String saveReturnId(KeyAreaCheckRenovationPatrolVO keyAreaCheckRenovationPatrolVO) {
        return saveReturnId(null, keyAreaCheckRenovationPatrolVO);
    }

    @Override
    public String saveReturnId(String token, KeyAreaCheckRenovationPatrolVO keyAreaCheckRenovationPatrolVO) {
        String keyAreaCheckRenovationPatrolId = UUIDUtil.getUUID();
        Map<String, Object> params = HashMapUtil.beanToMap(keyAreaCheckRenovationPatrolVO);
        params.put("keyAreaCheckRenovationPatrolId", keyAreaCheckRenovationPatrolId);
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        try {
            if (null != authentication) {
                if (StringUtils.isBlank(token)) {
                    setSaveInfo(params);
                } else {
                    setAppSaveInfo(token, params);
                }
            } else {
                String currentDate = DateUtil.getTime();
                params.put("creator", 1);
                params.put("gmtCreate", currentDate);
                params.put("modifier", 1);
                params.put("gmtModified", currentDate);
                params.put("isDelete", 0);
            }
        }catch (Exception e) {

        }
        if(null == params.get("creator")) {
            String currentDate = DateUtil.getTime();
            params.put("creator", 1);
            params.put("gmtCreate", currentDate);
            params.put("modifier", 1);
            params.put("gmtModified", currentDate);
            params.put("isDelete", 0);
        }
        keyAreaCheckRenovationPatrolDao.save(params);

        String creator = getUserData(params, keyAreaCheckRenovationPatrolVO);
        KeyAreaCheckRenovationDTO keyAreaCheckRenovationDTO = keyAreaCheckRenovationService.get(keyAreaCheckRenovationPatrolVO.getKeyAreaCheckRenovationId());
        saveKafka(keyAreaCheckRenovationPatrolId, keyAreaCheckRenovationPatrolVO, creator, keyAreaCheckRenovationDTO);
        return keyAreaCheckRenovationPatrolId;
    }

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

        deleteKafka(idsStr);
    }

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

    @Override
    public void update(String keyAreaCheckRenovationPatrolId, KeyAreaCheckRenovationPatrolVO keyAreaCheckRenovationPatrolVO) {
        update(null, keyAreaCheckRenovationPatrolId, keyAreaCheckRenovationPatrolVO);
    }

    @Override
    public void update(String token, String keyAreaCheckRenovationPatrolId, KeyAreaCheckRenovationPatrolVO keyAreaCheckRenovationPatrolVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(keyAreaCheckRenovationPatrolVO);
        params.put("keyAreaCheckRenovationPatrolId", keyAreaCheckRenovationPatrolId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        keyAreaCheckRenovationPatrolDao.update(params);

        KeyAreaCheckRenovationPatrolDTO keyAreaCheckRenovationPatrolDTO = get(keyAreaCheckRenovationPatrolId);
        params.put("creator", keyAreaCheckRenovationPatrolDTO.getCreator());
        String creator = getUserData(params, keyAreaCheckRenovationPatrolVO);
        KeyAreaCheckRenovationDTO keyAreaCheckRenovationDTO = keyAreaCheckRenovationService.get(keyAreaCheckRenovationPatrolVO.getKeyAreaCheckRenovationId());
        updateKafka(keyAreaCheckRenovationPatrolId, keyAreaCheckRenovationPatrolVO, creator, keyAreaCheckRenovationDTO);
    }

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

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

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

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

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

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

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

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

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

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

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

    @Override
    public void saveKafka(String keyAreaPatrolId, KeyAreaCheckRenovationPatrolVO keyAreaCheckRenovationPatrolVO, String creatorName, KeyAreaCheckRenovationDTO keyAreaCheckRenovationDTO) {
        KeyAreaCheckRenovationPatrol keyAreaCheckRenovationPatrol = setKafkaData(keyAreaCheckRenovationPatrolVO, creatorName, keyAreaCheckRenovationDTO);
        JSONObject jsonObject = new JSONObject();
        // 实体模板编码
        jsonObject.put("tableNumber", "C0019");
        // 数据主键
        jsonObject.put("uid", keyAreaPatrolId);
        // 设置数据操作方法
        // action: save/updete/delete
        jsonObject.put("action", "save");
        // 实体
        jsonObject.put("data", keyAreaCheckRenovationPatrol.saveCheckToJson());
        kafkaTemplate.send("tableSync", jsonObject.toJSONString());
    }

    @Override
    public void updateKafka(String keyAreaPatrolId, KeyAreaCheckRenovationPatrolVO keyAreaCheckRenovationPatrolVO, String creatorName, KeyAreaCheckRenovationDTO keyAreaCheckRenovationDTO) {
        KeyAreaCheckRenovationPatrol keyAreaCheckRenovationPatrol = setKafkaData(keyAreaCheckRenovationPatrolVO, creatorName, keyAreaCheckRenovationDTO);
        JSONObject jsonObject = new JSONObject();
        // 实体模板编码
        jsonObject.put("tableNumber", "C0019");
        // 数据主键
        jsonObject.put("uid", keyAreaPatrolId);
        // 设置数据操作方法
        // action: save/updete/delete
        jsonObject.put("action", "update");
        // 实体
        jsonObject.put("data", keyAreaCheckRenovationPatrol.updateToJson());
        kafkaTemplate.send("tableSync", jsonObject.toJSONString());
    }

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

    private KeyAreaCheckRenovationPatrol setKafkaData(KeyAreaCheckRenovationPatrolVO keyAreaCheckRenovationPatrolVO, String creatorName, KeyAreaCheckRenovationDTO keyAreaCheckRenovationDTO) {
        KeyAreaCheckRenovationPatrol keyAreaCheckRenovationPatrol = new KeyAreaCheckRenovationPatrol();
        keyAreaCheckRenovationPatrol.setkey_area_name(keyAreaCheckRenovationPatrolVO.getKeyAreaCheckRenovationName());
        keyAreaCheckRenovationPatrol.setpatrol_time(keyAreaCheckRenovationPatrolVO.getPatrolTime());
        keyAreaCheckRenovationPatrol.setcreator_name(creatorName);
        String locationAll = gridService.getLocationAll(keyAreaCheckRenovationDTO.getLocationCode());
        keyAreaCheckRenovationPatrol.setlocation_all(locationAll);
        keyAreaCheckRenovationPatrol.setlocation_code(keyAreaCheckRenovationDTO.getLocationCode());
        String locationArea = gridService.getLocationArea(keyAreaCheckRenovationDTO.getLocationCode());
        keyAreaCheckRenovationPatrol.setlocation_area(locationArea);
        return keyAreaCheckRenovationPatrol;
    }

    @Override
    public String getUserData(Map<String, Object> params, KeyAreaCheckRenovationPatrolVO keyAreaCheckRenovationPatrolVO) {
        String creator = params.get("creator").toString();
        UserDTO userDTO = userService.get(creator);
        if(null != userDTO) {
            creator = userDTO.getUserName();
        }
        String keyAreaId = keyAreaCheckRenovationPatrolVO.getKeyAreaCheckRenovationId();
        KeyAreaCheckRenovationDTO keyAreaCheckRenovationDTO = keyAreaCheckRenovationService.get(keyAreaId);
        if(null != keyAreaCheckRenovationDTO) {
            keyAreaCheckRenovationPatrolVO.setKeyAreaCheckRenovationName(keyAreaCheckRenovationDTO.getSecurityArea());
        }
        return creator;
    }

}