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

import cn.com.tenlion.systembase.dao.organization.IOrganizationDao;
import cn.com.tenlion.systembase.pojo.bos.organization.OrganizationBO;
import cn.com.tenlion.systembase.pojo.dtos.bigdta.OrgOrganization;
import cn.com.tenlion.systembase.pojo.dtos.organization.OrganizationDTO;
import cn.com.tenlion.systembase.pojo.pos.organization.OrganizationPO;
import cn.com.tenlion.systembase.pojo.vos.organization.OrganizationVO;
import cn.com.tenlion.systembase.service.grid.IGridService;
import cn.com.tenlion.systembase.service.organization.IOrganizationService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.exceptions.SearchException;
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.List;
import java.util.Map;

/**
 * @ClassName: OrganizationServiceImpl
 * @Description: 社会组织
 * @Author: CodeFactory
 * @Date: 2021-11-04 15:17:38
 * @Version: 3.0
 **/
@Service
public class OrganizationServiceImpl extends DefaultBaseService implements IOrganizationService {

    @Autowired
    private IOrganizationDao organizationDao;
    @Autowired
    private IGridService gridService;
    @Autowired
    private KafkaTemplate kafkaTemplate;

    @Override
    public void save(OrganizationVO organizationVO) {
        saveReturnId(organizationVO);
    }

    @Override
    public void save(String token, OrganizationVO organizationVO) {
        saveReturnId(token, organizationVO);
    }

    @Override
    public String saveReturnId(OrganizationVO organizationVO) {
        return saveReturnId(null, organizationVO);
    }

    @Override
    public String saveReturnId(String token, OrganizationVO organizationVO) {
        OrganizationPO organizationPO = getPOByBusinessRegNo(organizationVO.getBusinessRegNo());
        if (organizationPO != null) {
            throw new SearchException("组织机构代码已经存在");
        }

        String organizationId = UUIDUtil.getUUID();
        Map<String, Object> params = HashMapUtil.beanToMap(organizationVO);
        params.put("organizationId", organizationId);
        if (StringUtils.isBlank(token)) {
            setSaveInfo(params);
        } else {
            setAppSaveInfo(token, params);
        }
        organizationDao.save(params);

        saveKafka(organizationId, organizationVO);
        return organizationId;
    }

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

        deleteKafka(idsStr);
    }

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

    @Override
    public void update(String organizationId, OrganizationVO organizationVO) {
        update(null, organizationId, organizationVO);
    }

    @Override
    public void update(String token, String organizationId, OrganizationVO organizationVO) {
        OrganizationPO organizationPO = getPOByBusinessRegNo(organizationVO.getBusinessRegNo());
        if (organizationPO != null && !StringUtils.equals(organizationId, organizationPO.getOrganizationId())) {
            throw new SearchException("组织机构代码已经存在");
        }

        Map<String, Object> params = HashMapUtil.beanToMap(organizationVO);
        params.put("organizationId", organizationId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        organizationDao.update(params);

        updateKafka(organizationId, organizationVO);
    }

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

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

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

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

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

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

    @Override
    public OrganizationPO getPOByBusinessRegNo(String businessRegNo) {
        Map<String, Object> params = getHashMap(2);
        params.put("businessRegNo", businessRegNo);
        return getPO(params);
    }

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

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

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

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

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

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

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

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

    private OrgOrganization setKafkaData(OrganizationVO organizationVO) {
        OrgOrganization orgOrganization = new OrgOrganization();
        // 名称
        orgOrganization.setorganization_name(organizationVO.getOrganizationName());
        // 类型名称
        orgOrganization.setorganization_type_name(organizationVO.getOrganizationTypeName());
        // 区域全名称(/隔开)
        String locationAll = gridService.getLocationAll(organizationVO.getAreaCode());
        orgOrganization.setlocation_all(locationAll);
        // 区域编码
        orgOrganization.setlocation_code(organizationVO.getAreaCode());
        // 区域旗县区名称
        String locationArea = gridService.getLocationArea(organizationVO.getAreaCode());
        orgOrganization.setlocation_area(locationArea);
        // 经度
        orgOrganization.setlongitude(organizationVO.getOrganizationLng());
        // 纬度
        orgOrganization.setlatitude(organizationVO.getOrganizationLat());
        // 网格名称
        orgOrganization.setgrid_name(organizationVO.getGridName());
        // 网格编码
        orgOrganization.setgrid_code(organizationVO.getGridCode());
        return orgOrganization;
    }

}