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

import cn.com.tenlion.systembase.dao.logisticssafe.ILogisticsSafeDao;
import cn.com.tenlion.systembase.pojo.bos.logisticssafe.LogisticsSafeBO;
import cn.com.tenlion.systembase.pojo.dtos.grid.GridDTO;
import cn.com.tenlion.systembase.pojo.dtos.logisticssafe.LogisticsSafeDTO;
import cn.com.tenlion.systembase.pojo.pos.logisticssafe.LogisticsSafePO;
import cn.com.tenlion.systembase.pojo.vos.logisticssafe.LogisticsSafeVO;
import cn.com.tenlion.systembase.service.grid.IGridService;
import cn.com.tenlion.systembase.service.logisticssafe.ILogisticsSafeService;
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: LogisticsSafeServiceImpl
 * @Description: 寄递物流安全管理
 * @Author: CodeFactory
 * @Date: 2021-10-19 14:16:33
 * @Version: 3.0
 **/
@Service
public class LogisticsSafeServiceImpl extends DefaultBaseService implements ILogisticsSafeService {

    @Autowired
    private ILogisticsSafeDao logisticsSafeDao;
    @Autowired
    private IGridService gridService;

    @Override
    public void save(LogisticsSafeVO logisticsSafeVO) {
        saveReturnId(logisticsSafeVO);
    }

    @Override
    public void save(String token, LogisticsSafeVO logisticsSafeVO) {
        saveReturnId(token, logisticsSafeVO);
    }

    @Override
    public String saveReturnId(LogisticsSafeVO logisticsSafeVO) {
        return saveReturnId(null, logisticsSafeVO);
    }

    @Override
    public String saveReturnId(String token, LogisticsSafeVO logisticsSafeVO) {
        String logisticsSafeId = UUIDUtil.getUUID();
        Map<String, Object> params = HashMapUtil.beanToMap(logisticsSafeVO);
        params.put("logisticsSafeId", logisticsSafeId);
        if (StringUtils.isBlank(token)) {
            setSaveInfo(params);
        } else {
            setAppSaveInfo(token, params);
        }
        // 由于HashMapUtil自动转换对象时，会将xMachineCount中的x大写，所以执行重新put操作
        params.put("xMachineCount", logisticsSafeVO.getxMachineCount());
        params.remove("XMachineCount");
        logisticsSafeDao.save(params);
        return logisticsSafeId;
    }

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

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

    @Override
    public void update(String logisticsSafeId, LogisticsSafeVO logisticsSafeVO) {
        update(null, logisticsSafeId, logisticsSafeVO);
    }

    @Override
    public void update(String token, String logisticsSafeId, LogisticsSafeVO logisticsSafeVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(logisticsSafeVO);
        params.put("logisticsSafeId", logisticsSafeId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        // 由于HashMapUtil自动转换对象时，会将xMachineCount中的x大写，所以执行重新put操作
        params.put("xMachineCount", logisticsSafeVO.getxMachineCount());
        params.remove("XMachineCount");
        logisticsSafeDao.update(params);
    }

    @Override
    public LogisticsSafeDTO get(Map<String, Object> params) {
        LogisticsSafeDTO logisticsSafeDTO = logisticsSafeDao.get(params);
        if(null != logisticsSafeDTO) {
            String businessNature = logisticsSafeDTO.getBusinessNature();
            String businessNatureCode = logisticsSafeDTO.getBusinessNatureCode();
            if(null != businessNature && null != businessNatureCode) {
                String businessNatureValue = "";
                String[] businessNatureArr = businessNature.split(",");
                String[] businessNatureCodeArr = businessNatureCode.split(",");
                for(int i=0;i<businessNatureArr.length;i++) {
                    businessNatureValue += businessNatureArr[i] + "," + businessNatureCodeArr[i] + "_";
                }
                businessNatureValue = businessNatureValue.substring(0, businessNatureValue.length()-1);
                logisticsSafeDTO.setBusinessNatureValue(businessNatureValue);
            }
            GridDTO gridDTO = gridService.get(logisticsSafeDTO.getGrid());
            if(null != gridDTO) {
                logisticsSafeDTO.setGridName(gridDTO.getGridName());
            }
        }
        return logisticsSafeDTO;
    }

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

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

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

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

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

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

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

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

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

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

}