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

import cn.com.tenlion.systembase.dao.massprevention.IMassPreventionDao;
import cn.com.tenlion.systembase.pojo.bos.massprevention.MassPreventionBO;
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.massprevention.MassPreventionDTO;
import cn.com.tenlion.systembase.pojo.pos.massprevention.MassPreventionPO;
import cn.com.tenlion.systembase.pojo.vos.massprevention.MassPreventionVO;
import cn.com.tenlion.systembase.pojo.vos.school.SchoolVO;
import cn.com.tenlion.systembase.service.grid.IGridService;
import cn.com.tenlion.systembase.service.massprevention.IMassPreventionService;
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: MassPreventionServiceImpl
 * @Description: 群防群治组织
 * @Author: CodeFactory
 * @Date: 2021-10-12 10:09:38
 * @Version: 3.0
 **/
@Service
public class MassPreventionServiceImpl extends DefaultBaseService implements IMassPreventionService {

    @Autowired
    private IMassPreventionDao massPreventionDao;
    @Autowired
    private IGridService gridService;

    @Override
    public void save(MassPreventionVO massPreventionVO) {
        saveReturnId(massPreventionVO);
    }

    @Override
    public void save(String token, MassPreventionVO massPreventionVO) {
        saveReturnId(token, massPreventionVO);
    }

    @Override
    public String saveReturnId(MassPreventionVO massPreventionVO) {
        return saveReturnId(null, massPreventionVO);
    }

    @Override
    public String saveReturnId(String token, MassPreventionVO massPreventionVO) {
        String massPreventionId = UUIDUtil.getUUID();
        Map<String, Object> params = HashMapUtil.beanToMap(massPreventionVO);
        params.put("massPreventionId", massPreventionId);
        if (StringUtils.isBlank(token)) {
            setSaveInfo(params);
        } else {
            setAppSaveInfo(token, params);
        }
        massPreventionDao.save(params);
        return massPreventionId;
    }

    @Override
    public void remove(List<String> ids) {
        remove(null, ids);
    }

    @Override
    public void remove(String token, List<String> ids) {
        Map<String, Object> params = getHashMap(2);
        params.put("massPreventionIds", ids);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        massPreventionDao.remove(params);
    }

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

    @Override
    public void update(String massPreventionId, MassPreventionVO massPreventionVO) {
        update(null, massPreventionId, massPreventionVO);
    }

    @Override
    public void update(String token, String massPreventionId, MassPreventionVO massPreventionVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(massPreventionVO);
        params.put("massPreventionId", massPreventionId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        massPreventionDao.update(params);
    }

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

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

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

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

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

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

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

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

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

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

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

}