package com.yunfeiyun.agmp.fms.service.impl;

import com.yunfeiyun.agmp.common.utils.DateUtils;
import com.yunfeiyun.agmp.common.utils.SecurityUtils;
import com.yunfeiyun.agmp.fms.domain.FmsBlock;
import com.yunfeiyun.agmp.fms.domain.FmsPlan;
import com.yunfeiyun.agmp.fms.mapper.FmsBlockMapper;
import com.yunfeiyun.agmp.fms.mapper.FmsPlanMapper;
import com.yunfeiyun.agmp.fms.domain.reqvo.FmsPlanReqVo;
import com.yunfeiyun.agmp.fms.domain.resvo.FmsPlanListOfTaskStatusResVo;
import com.yunfeiyun.agmp.fms.domain.resvo.FmsPlanResVo;
import com.yunfeiyun.agmp.fms.service.IFmsPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 种植计划Service业务层处理
 * 
 * @author 杨晓辉
 * @date 2023-05-18
 */
@Service
public class FmsPlanServiceImpl implements IFmsPlanService
{
    @Autowired
    private FmsPlanMapper fmsPlanMapper;
    @Autowired
    private FmsBlockMapper fmsBlockMapper;

    /**
     * 查询种植计划
     * 
     * @param planId 种植计划主键
     * @return 种植计划
     */
    @Override
    public FmsPlanResVo selectFmsPlanByPlanId(String planId)
    {
        return fmsPlanMapper.selectFmsPlanByPlanId(planId);
    }

    @Override
    public FmsPlanResVo selectFmsPlanRecordByPlanId(String planId) {
        return fmsPlanMapper.selectFmsPlanRecordByPlanId(planId);
    }

    @Override
    public BigDecimal selectSumPlanArea(FmsPlanReqVo reqVo) {
        return fmsPlanMapper.selectSumPlanArea(reqVo);
    }

    @Override
    public int selectCountFmsPlanByBlockId(String blockId) {
        return fmsPlanMapper.selectCountFmsPlanByBlockId(blockId);
    }

    @Override
    public int selectCountFmsPlan(FmsPlanReqVo reqVo) {
        return fmsPlanMapper.selectCountFmsPlan(reqVo);
    }

    @Override
    public List<FmsPlanResVo> selectPlanByCropName(FmsPlanReqVo reqVo) {
        return fmsPlanMapper.selectPlanByCropName(reqVo);
    }

    @Override
    public List<FmsPlanResVo> selectPlanByCropNameAndType(FmsPlanReqVo reqVo) {
        return fmsPlanMapper.selectPlanByCropNameAndType(reqVo);
    }

    /**
     * 查询种植计划列表
     * 
     * @param fmsPlan 种植计划
     * @return 种植计划
     */
    @Override
    public List<FmsPlan> selectFmsPlanList(FmsPlan fmsPlan)
    {
        return fmsPlanMapper.selectFmsPlanList(fmsPlan);
    }

    @Override
    public List<FmsPlanResVo> selectPlanList(FmsPlanReqVo reqVo) {
        return fmsPlanMapper.selectPlanList(reqVo);
    }

    @Override
    public List<FmsPlan> selectPlanListScheduler(FmsPlanReqVo reqVo) {
        return fmsPlanMapper.selectPlanListScheduler(reqVo);
    }

    /**
     * 新增种植计划
     * 
     * @param fmsPlan 种植计划
     * @return 结果
     */
    @Override
    public int insertFmsPlan(FmsPlan fmsPlan)
    {
        fmsPlan.setPlanId(fmsPlan.getId());
        return fmsPlanMapper.insertFmsPlan(fmsPlan);
    }

    @Transactional
    @Override
    public int insertFmsPlan(FmsPlan fmsPlan, FmsBlock fmsBlock) {
        fmsPlan.setPlanId(fmsPlan.getId());
        fmsPlan.setPlanCreator(SecurityUtils.getUserId());
        fmsPlan.setPlanCreateddate(DateUtils.dateTimeNow());
        int i = fmsPlanMapper.insertFmsPlan(fmsPlan);
        fmsBlockMapper.updateFmsBlock(fmsBlock);

        return i;
    }

    /**
     * 修改种植计划
     * 
     * @param fmsPlan 种植计划
     * @return 结果
     */
    @Override
    public int updateFmsPlan(FmsPlan fmsPlan)
    {
        fmsPlan.setPlanModifier(SecurityUtils.getUserId());
        fmsPlan.setPlanModifieddate(DateUtils.dateTimeNow());
        return fmsPlanMapper.updateFmsPlan(fmsPlan);
    }

    @Transactional
    @Override
    public int updateFmsPlan(FmsPlan fmsPlan, FmsBlock fmsBlock) {
        int i = fmsPlanMapper.updateFmsPlan(fmsPlan);
        fmsBlockMapper.updateFmsBlock(fmsBlock);
        return i;
    }

    @Override
    public int updateFmsPlanBatch(List<FmsPlan> fmsPlans) {
        return fmsPlanMapper.updateFmsPlanBatch(fmsPlans);
    }

    @Transactional
    @Override
    public int updateFmsPlanBatch(List<FmsPlan> fmsPlans, List<FmsBlock> fmsBlocks) {
        int i = fmsPlanMapper.updateFmsPlanBatch(fmsPlans);
        fmsBlockMapper.updateFmsBlockBatch(fmsBlocks);
        return i;
    }

    /**
     * 批量删除种植计划
     * 
     * @param planIds 需要删除的种植计划主键
     * @return 结果
     */
    @Override
    public int deleteFmsPlanByPlanIds(String[] planIds)
    {
        return fmsPlanMapper.deleteFmsPlanByPlanIds(planIds);
    }

    /**
     * 删除种植计划信息
     * 
     * @param planId 种植计划主键
     * @return 结果
     */
    @Override
    public int deleteFmsPlanByPlanId(String planId)
    {
        return fmsPlanMapper.deleteFmsPlanByPlanId(planId);
    }

    @Override
    public List<FmsPlanListOfTaskStatusResVo> selectPlanListOfTaskStatus(Map<String, Object> param) {
        return fmsPlanMapper.selectPlanListOfTaskStatus(param);
    }

}
