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

import cn.com.tenlion.systembase.dao.linecase.ILineCaseDao;
import cn.com.tenlion.systembase.pojo.bos.linecase.LineCaseBO;
import cn.com.tenlion.systembase.pojo.dtos.grid.GridDTO;
import cn.com.tenlion.systembase.pojo.dtos.linecase.LineCaseDTO;
import cn.com.tenlion.systembase.pojo.pos.linecase.LineCasePO;
import cn.com.tenlion.systembase.pojo.vos.linecase.LineCaseVO;
import cn.com.tenlion.systembase.service.grid.IGridService;
import cn.com.tenlion.systembase.service.linecase.ILineCaseService;
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: LineCaseServiceImpl
 * @Description: 涉及线、路案（事）件
 * @Author: CodeFactory
 * @Date: 2021-10-13 14:58:15
 * @Version: 3.0
 **/
@Service
public class LineCaseServiceImpl extends DefaultBaseService implements ILineCaseService {

    @Autowired
    private ILineCaseDao lineCaseDao;
    @Autowired
    private IGridService gridService;

    @Override
    public void save(LineCaseVO lineCaseVO) {
        saveReturnId(lineCaseVO);
    }

    @Override
    public void save(String token, LineCaseVO lineCaseVO) {
        saveReturnId(token, lineCaseVO);
    }

    @Override
    public String saveReturnId(LineCaseVO lineCaseVO) {
        return saveReturnId(null, lineCaseVO);
    }

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

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

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

    @Override
    public void update(String lineCaseId, LineCaseVO lineCaseVO) {
        update(null, lineCaseId, lineCaseVO);
    }

    @Override
    public void update(String token, String lineCaseId, LineCaseVO lineCaseVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(lineCaseVO);
        params.put("lineCaseId", lineCaseId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        lineCaseDao.update(params);
    }

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

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

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

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

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

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

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

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

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

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

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

}