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

import cn.com.tenlion.systembase.dao.masspreventionrank.IMassPreventionRankDao;
import cn.com.tenlion.systembase.pojo.bos.masspreventionrank.MassPreventionRankBO;
import cn.com.tenlion.systembase.pojo.dtos.masspreventionrank.MassPreventionRankDTO;
import cn.com.tenlion.systembase.pojo.pos.masspreventionrank.MassPreventionRankPO;
import cn.com.tenlion.systembase.pojo.vos.masspreventionrank.MassPreventionRankVO;
import cn.com.tenlion.systembase.service.grid.IGridService;
import cn.com.tenlion.systembase.service.masspreventionrank.IMassPreventionRankService;
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: MassPreventionRankServiceImpl
 * @Description: 群防群治队伍
 * @Author: CodeFactory
 * @Date: 2021-10-12 14:40:57
 * @Version: 3.0
 **/
@Service
public class MassPreventionRankServiceImpl extends DefaultBaseService implements IMassPreventionRankService {

    @Autowired
    private IMassPreventionRankDao massPreventionRankDao;

    @Override
    public void save(MassPreventionRankVO massPreventionRankVO) {
        saveReturnId(massPreventionRankVO);
    }

    @Override
    public void save(String token, MassPreventionRankVO massPreventionRankVO) {
        saveReturnId(token, massPreventionRankVO);
    }

    @Override
    public String saveReturnId(MassPreventionRankVO massPreventionRankVO) {
        return saveReturnId(null, massPreventionRankVO);
    }

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

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

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

    @Override
    public void update(String massPreventionRankId, MassPreventionRankVO massPreventionRankVO) {
        update(null, massPreventionRankId, massPreventionRankVO);
    }

    @Override
    public void update(String token, String massPreventionRankId, MassPreventionRankVO massPreventionRankVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(massPreventionRankVO);
        params.put("massPreventionRankId", massPreventionRankId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        massPreventionRankDao.update(params);
    }

    @Override
    public MassPreventionRankDTO get(Map<String, Object> params) {
        MassPreventionRankDTO massPreventionRankDTO = massPreventionRankDao.get(params);
        if(null != massPreventionRankDTO) {
            String professionalExpertise = massPreventionRankDTO.getProfessionalExpertise();
            String professionalExpertiseCode = massPreventionRankDTO.getProfessionalExpertiseCode();
            if(null != professionalExpertise && null != professionalExpertiseCode) {
                String professionalExpertiseValue = "";
                String[] professionalExpertiseArr = professionalExpertise.split(",");
                String[] professionalExpertiseCodeArr = professionalExpertiseCode.split(",");
                for(int i=0;i<professionalExpertiseArr.length;i++) {
                    professionalExpertiseValue += professionalExpertiseArr[i] + "," + professionalExpertiseCodeArr[i] + "_";
                }
                professionalExpertiseValue = professionalExpertiseValue.substring(0, professionalExpertiseValue.length()-1);
                massPreventionRankDTO.setProfessionalExpertiseValue(professionalExpertiseValue);
            }
        }
        return massPreventionRankDTO;
    }

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

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

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

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

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

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

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

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

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

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

}