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

import cn.com.tenlion.systembase.dao.keyplace.IKeyPlaceDao;
import cn.com.tenlion.systembase.pojo.bos.keyplace.KeyPlaceBO;
import cn.com.tenlion.systembase.pojo.dtos.bigdta.KeyPlace;
import cn.com.tenlion.systembase.pojo.dtos.grid.GridDTO;
import cn.com.tenlion.systembase.pojo.dtos.keyplace.KeyPlaceDTO;
import cn.com.tenlion.systembase.pojo.pos.keyplace.KeyPlacePO;
import cn.com.tenlion.systembase.pojo.vos.keyplace.KeyPlaceVO;
import cn.com.tenlion.systembase.service.grid.IGridService;
import cn.com.tenlion.systembase.service.keyplace.IKeyPlaceService;
import cn.com.tenlion.systembase.util.VerifyUtil;
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: KeyPlaceServiceImpl
 * @Description: 重点场所
 * @Author: CodeFactory
 * @Date: 2021-11-16 11:25:17
 * @Version: 3.0
 **/
@Service
public class KeyPlaceServiceImpl extends DefaultBaseService implements IKeyPlaceService {

    @Autowired
    private IKeyPlaceDao keyPlaceDao;
    @Autowired
    private IGridService gridService;
    @Autowired
    private KafkaTemplate kafkaTemplate;

    @Override
    public void save(KeyPlaceVO keyPlaceVO) {
        saveReturnId(keyPlaceVO);
    }

    @Override
    public void save(String token, KeyPlaceVO keyPlaceVO) {
        saveReturnId(token, keyPlaceVO);
    }

    @Override
    public String saveReturnId(KeyPlaceVO keyPlaceVO) {
        return saveReturnId(null, keyPlaceVO);
    }

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

        saveKafka(keyPlaceId, keyPlaceVO);
        return keyPlaceId;
    }

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

        deleteKafka(idsStr);
    }

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

    @Override
    public void update(String keyPlaceId, KeyPlaceVO keyPlaceVO) {
        update(null, keyPlaceId, keyPlaceVO);
    }

    @Override
    public void update(String token, String keyPlaceId, KeyPlaceVO keyPlaceVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(keyPlaceVO);
        params.put("keyPlaceId", keyPlaceId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        keyPlaceDao.update(params);

        updateKafka(keyPlaceId, keyPlaceVO);
    }

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

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

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

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

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

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

    @Override
    public List<KeyPlaceDTO> list(Map<String, Object> params) {
        if(params.get("areaCodeLike") != null && !"".equals(params.get("areaCodeLike").toString())){
            String areaCoed = params.get("areaCodeLike").toString();
            String[] areaCodeSplit = VerifyUtil.splitStringByLength(areaCoed, 2);
            String areaCodeLike = "";
            for(int i = 5 ; i >= 0; i--){
                if(!"00".equals(areaCodeSplit[i])){
                    areaCodeLike = areaCoed.substring(0, (i + 1) * 2 );
                    break;
                }
            }
            params.put("areaCodeLike",areaCodeLike);
        }
        return keyPlaceDao.list(params);
    }

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

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

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

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

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

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

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

    private KeyPlace setKafkaData(KeyPlaceVO keyPlaceVO) {
        KeyPlace keyPlace = new KeyPlace();
        // 场所名称
        keyPlace.setplace_name(keyPlaceVO.getPlaceName());
        // 场所类型
        keyPlace.setplace_type_name(keyPlaceVO.getPlaceTypeName());
        // 详细类型
        keyPlace.setdetail_type_name(keyPlaceVO.getDetailTypeName());
        // 详细地址
        keyPlace.setaddress(keyPlaceVO.getAddress());
        // 区域全名称(/隔开)
        String locationAll = gridService.getLocationAll(keyPlaceVO.getAreaCode());
        keyPlace.setlocation_all(locationAll);
        // 区域编码
        keyPlace.setlocation_code(keyPlaceVO.getAreaCode());
        // 区域旗县区名称
        String locationArea = gridService.getLocationArea(keyPlaceVO.getAreaCode());
        keyPlace.setlocation_area(locationArea);
        // 经度
        keyPlace.setlongitude(keyPlaceVO.getLongitude());
        // 纬度
        keyPlace.setlatitude(keyPlaceVO.getLatitude());
        GridDTO gridDTO = gridService.get(keyPlaceVO.getGrid());
        if(null != gridDTO) {
            // 网格名称
            keyPlace.setgrid_name(gridDTO.getGridName());
            // 网格编码
            keyPlace.setgrid_code(gridDTO.getGridCode());
        }else {
            // 网格名称
            keyPlace.setgrid_name("暂无数据");
            // 网格编码
            keyPlace.setgrid_code("暂无数据");
        }
        return keyPlace;
    }

}