package com.unis.purchaseAndSalesSupervision.modular.yjbj.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.unis.purchaseAndSalesSupervision.modular.yjbj.param.*;
import com.unis.purchaseAndSalesSupervision.modular.yjbj.entity.StorageJkqWarning;
import com.unis.purchaseAndSalesSupervision.modular.yjbj.entity.StorageRotationWarning;
import com.unis.purchaseAndSalesSupervision.modular.yjbj.entity.ZqlTable;
import com.unis.purchaseAndSalesSupervision.modular.yjbj.mapper.YjbjMapper;
import com.unis.purchaseAndSalesSupervision.modular.yjbj.service.YjbjService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class YjbjServiceImpl implements YjbjService {

    @Resource
    private YjbjMapper yjbjMapper;

    @Override
    public Page<ZqlTable> selectZqlListPage(ZqlPageParam yjbjPageParam) {

        if (StringUtils.isNotBlank(yjbjPageParam.getStartDate())) {
            yjbjPageParam.setStartDate(yjbjPageParam.getStartDate()+" 00:00:00");
        }
        if (StringUtils.isNotBlank(yjbjPageParam.getEndDate())) {
            yjbjPageParam.setEndDate(yjbjPageParam.getEndDate()+" 23:59:59");
        }

        Page<ZqlTable> page = new Page<>(yjbjPageParam.getCurrent(), yjbjPageParam.getSize());
        return yjbjMapper.selectZqlListPage(page, yjbjPageParam);
    }

    @Override
    public ZqlTable queryZqlById(ZqlIdParam zqlIdParam) {
        return yjbjMapper.queryZqlById(zqlIdParam.getId());
    }

    @Override
    public Page<StorageRotationWarning> selectLhList(StorageRotationWarningPageParam storageRotationWarningPageParam) {
        Map<String, Object> paramMap = new HashMap();
        if (null != storageRotationWarningPageParam.getOrgId()) {
            paramMap.put("orgId", storageRotationWarningPageParam.getOrgId()); //库ID
        }
        if (null != storageRotationWarningPageParam.getLspz()) {
            paramMap.put("lspz", storageRotationWarningPageParam.getLspz());  //粮食品种ID
        }
        if (null != storageRotationWarningPageParam.getCh()) {
            paramMap.put("ch", storageRotationWarningPageParam.getCh());  //仓房ID
        }
        if (StringUtils.isNotBlank(storageRotationWarningPageParam.getRkrq())) {
            paramMap.put("startTime", storageRotationWarningPageParam.getRkrq()+" 00:00:00");
            paramMap.put("endTime", storageRotationWarningPageParam.getRkrq()+" 23:59:59");
        }

        Page<StorageRotationWarning> page = new Page<>(storageRotationWarningPageParam.getCurrent(), storageRotationWarningPageParam.getSize());
        return yjbjMapper.selectLhList(page, paramMap);
    }

    @Override
    public StorageRotationWarning queryLhById(StorageRotationWarningIdParam storageRotationWarningIdParam) {
        return yjbjMapper.queryLhById(storageRotationWarningIdParam.getId());
    }

    @Override
    public Page<StorageJkqWarning> selectCjkqList(StorageJkqWarningPageParam storageJkqWarningPageParam) {

        Map<String, Object> paramMap = new HashMap();
        if (null != storageJkqWarningPageParam.getOrgId()) {
            paramMap.put("orgId", storageJkqWarningPageParam.getOrgId()); //库ID
        }
        if (StringUtils.isNotBlank(storageJkqWarningPageParam.getLhnd())) {
            paramMap.put("lhnd", storageJkqWarningPageParam.getLhnd());
        }
        if (StringUtils.isNotBlank(storageJkqWarningPageParam.getJkqzt())) {
            paramMap.put("jkqzt", storageJkqWarningPageParam.getJkqzt());
        }

        Page<StorageJkqWarning> page = new Page<>(storageJkqWarningPageParam.getCurrent(), storageJkqWarningPageParam.getSize());
        return yjbjMapper.selectCjkqList(page, paramMap);
    }

    @Override
    public StorageJkqWarning queryCjkqById(StorageJkqWarningIdParam storageJkqWarningIdParam) {
        return yjbjMapper.queryCjkqById(storageJkqWarningIdParam.getId());
    }

    @Override
    public IPage<List<Map<String, Object>>> getXwcjfxList(XwcjfxPageParam xwcjfxPageParam) {

        return yjbjMapper.getXwcjfxList(new Page<>(xwcjfxPageParam.getCurrent(), xwcjfxPageParam.getSize()), xwcjfxPageParam);

    }

    @Override
    public IPage<List<Map<String, Object>>> getCkjhzxjgList(XwcjfxPageParam xwcjfxPageParam) {
        return yjbjMapper.getCkjhzxjgList(new Page<>(xwcjfxPageParam.getCurrent(), xwcjfxPageParam.getSize()), xwcjfxPageParam);
    }
}
