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

import cn.com.tenlion.systembase.dao.homicide.IHomicideDao;
import cn.com.tenlion.systembase.pojo.bos.homicide.HomicideBO;
import cn.com.tenlion.systembase.pojo.dtos.grid.GridDTO;
import cn.com.tenlion.systembase.pojo.dtos.homicide.HomicideDTO;
import cn.com.tenlion.systembase.pojo.pos.homicide.HomicidePO;
import cn.com.tenlion.systembase.pojo.vos.homicide.HomicideVO;
import cn.com.tenlion.systembase.service.grid.IGridService;
import cn.com.tenlion.systembase.service.homicide.IHomicideService;
import cn.com.tenlion.systembase.util.VerifyUtil;
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.stereotype.Service;

import java.util.*;

/**
 * @ClassName: HomicideServiceImpl
 * @Description: 命案基本信息
 * @Author: CodeFactory
 * @Date: 2021-10-19 10:20:09
 * @Version: 3.0
 **/
@Service
public class HomicideServiceImpl extends DefaultBaseService implements IHomicideService {

    @Autowired
    private IHomicideDao homicideDao;
    @Autowired
    private IGridService gridService;

    @Override
    public void save(HomicideVO homicideVO) {
        saveReturnId(homicideVO);
    }

    @Override
    public void save(String token, HomicideVO homicideVO) {
        saveReturnId(token, homicideVO);
    }

    @Override
    public String saveReturnId(HomicideVO homicideVO) {
        return saveReturnId(null, homicideVO);
    }

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

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

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

    @Override
    public void update(String homicideId, HomicideVO homicideVO) {
        update(null, homicideId, homicideVO);
    }

    @Override
    public void update(String token, String homicideId, HomicideVO homicideVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(homicideVO);
        params.put("homicideId", homicideId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        homicideDao.update(params);
    }

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

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

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

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

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

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

    @Override
    public List<HomicideDTO> 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 homicideDao.list(params);
    }

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

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

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

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

}