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

import com.yunfeiyun.agmp.common.constant.ErrorCode;
import com.yunfeiyun.agmp.common.core.domain.AjaxResult;
import com.yunfeiyun.agmp.common.utils.StringUtils;
import com.yunfeiyun.agmp.common.web.system.domain.SysRes;
import com.yunfeiyun.agmp.common.web.system.mapper.SysResMapper;
import com.yunfeiyun.agmp.common.web.system.service.ISysResService;
import com.yunfeiyun.agmp.fms.domain.FmsBlock;
import com.yunfeiyun.agmp.fms.domain.FmsLand;
import com.yunfeiyun.agmp.fms.mapper.FmsBlockMapper;
import com.yunfeiyun.agmp.fms.mapper.FmsCropMapper;
import com.yunfeiyun.agmp.fms.mapper.FmsLandMapper;
import com.yunfeiyun.agmp.fms.domain.reqvo.FmsBlockReqVo;
import com.yunfeiyun.agmp.fms.domain.resvo.FmsBlockResVo;
import com.yunfeiyun.agmp.fms.service.IFmsBlockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 地块Service业务层处理
 * 
 * @author 杨晓辉
 * @date 2023-05-15
 */
@Service
public class FmsBlockServiceImpl implements IFmsBlockService 
{
    @Autowired
    private FmsBlockMapper fmsBlockMapper;
    @Autowired
    private FmsLandMapper fmsLandMapper;
    @Resource
    private SysResMapper sysResMapper;
    @Resource
    private ISysResService sysResService;
    @Autowired
    private FmsCropMapper fmsCropMapper;

    /**
     * 查询地块
     * 
     * @param blockId 地块主键
     * @return 地块
     */
    @Override
    public FmsBlock selectFmsBlockByBlockId(String blockId)
    {
        return fmsBlockMapper.selectFmsBlockByBlockId(blockId);
    }

    @Override
    public FmsBlockResVo selectFmsBlockInfoByBlockId(String blockId) {
        return fmsBlockMapper.selectFmsBlockInfoByBlockId(blockId);
    }

    /**
     * 查询地块列表
     * 
     * @param fmsBlock 地块
     * @return 地块
     */
    @Override
    public List<FmsBlock> selectFmsBlockList(FmsBlock fmsBlock)
    {
        return fmsBlockMapper.selectFmsBlockList(fmsBlock);
    }

    /**
     * 新增地块
     * 
     * @param fmsBlock 地块
     * @return 结果
     */
    @Transactional
    @Override
    public AjaxResult insertFmsBlock(FmsBlock fmsBlock, FmsLand fmsLand)
    {
        //新增地块时，对所属基地的地块数进行更新维护
        fmsBlock.setBlockId(fmsBlock.getId());
        fmsBlock.setBlockAreaplant(new BigDecimal("0"));
        int i = fmsBlockMapper.insertFmsBlock(fmsBlock);

        Integer count = fmsBlockMapper.selectCountByLandId(fmsBlock.getLandId());
        //地块面积不能大于基地面积
        if (count > 0) {
            FmsLand fmsLand1 = new FmsLand();
            fmsLand1.setLandId(fmsBlock.getLandId());
            BigDecimal sum = fmsBlockMapper.selectSumAreaByLandId(fmsLand1);
            if (sum.compareTo(fmsLand.getLandArea()) == 1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

//                return AjaxResult.error(ErrorCode.INVALID_PARAMETER.getCode());
                return AjaxResult.error(ErrorCode.INVALID_PARAMETER_AREA.getCode(), ErrorCode.INVALID_PARAMETER_AREA.getMessage());
            }
        }
        fmsLand.setLandBlockcount(Long.valueOf(count));

        fmsLandMapper.updateFmsLand(fmsLand);

        String resIdsStr = fmsBlock.getResIds();
        if (!StringUtils.isEmpty(resIdsStr)) {
            String[] resIds = resIdsStr.split(",");
            for (String resId : resIds) {
                SysRes sysRes = sysResMapper.selectSysResByResId(resId);
                sysRes.setResBusId(fmsBlock.getBlockId());
                sysResMapper.updateSysRes(sysRes);
            }
        }
        return AjaxResult.success();
    }

    /**
     * 修改地块
     * 
     * @param fmsBlock 地块
     * @return 结果
     */
    @Transactional
    @Override
    public AjaxResult updateFmsBlock(FmsBlock fmsBlock, FmsLand fmsLand)
    {
        fmsBlockMapper.updateFmsBlock(fmsBlock);
        FmsLand fmsLand1 = new FmsLand();
        fmsLand1.setLandId(fmsBlock.getLandId());
        BigDecimal sum = fmsBlockMapper.selectSumAreaByLandId(fmsLand1);
        if (sum.compareTo(fmsLand.getLandArea()) == 1) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error(ErrorCode.INVALID_PARAMETER.getCode(), ErrorCode.INVALID_PARAMETER_AREA.getMessage());
        }

        SysRes sysRes = new SysRes();
        sysRes.setResBusId(fmsBlock.getBlockId());
        List<SysRes> sysResList = sysResMapper.selectSysResList(sysRes);
        List<String> resIdsOld = new ArrayList<>();
        for (SysRes res : sysResList) {
            resIdsOld.add(res.getResId());
        }
        String resIdsStr = fmsBlock.getResIds();
        List<String> resIdsNew = new ArrayList<>();
        if (!StringUtils.isEmpty(resIdsStr)) {
            String[] resIds = resIdsStr.split(",");
            resIdsNew = Arrays.asList(resIds);
        }
        sysResService.updateSysRes(resIdsOld, resIdsNew, fmsBlock.getBlockId());
        return AjaxResult.success();
    }

    /**
     * 批量删除地块
     * 
     * @param blockIds 需要删除的地块主键
     * @return 结果
     */
    @Transactional
    @Override
    public AjaxResult deleteFmsBlockByBlockIds(String[] blockIds)
    {
        //获取批量删除地块的所包含的基地
        List<FmsLand> fmsLands = fmsBlockMapper.selectFmsLandByBlockIds(blockIds);
        fmsBlockMapper.deleteFmsBlockByBlockIds(blockIds);
        for (FmsLand fmsLand : fmsLands) {
            String landId = fmsLand.getLandId();
            Integer count = fmsBlockMapper.selectCountByLandId(landId);
            fmsLand.setLandBlockcount(Long.valueOf(count));
//            fmsLandMapper.updateFmsLand(fmsLand);
        }
        fmsLandMapper.updateFmsLandBatch(fmsLands);
        return AjaxResult.success();
    }

    /**
     * 删除地块信息
     * 
     * @param blockId 地块主键
     * @return 结果
     */
    @Override
    public int deleteFmsBlockByBlockId(String blockId)
    {
        return fmsBlockMapper.deleteFmsBlockByBlockId(blockId);
    }

    @Override
    public Integer selectCountByLandId(String landId) {
        return fmsBlockMapper.selectCountByLandId(landId);
    }

    @Override
    public BigDecimal selectSumAreaByLandId(FmsLand fmsLand) {
        return fmsBlockMapper.selectSumAreaByLandId(fmsLand);
    }

    @Override
    public BigDecimal selectSumArea(FmsBlockReqVo reqVo) {
        return fmsBlockMapper.selectSumArea(reqVo);
    }

    @Override
    public List<FmsBlockResVo> selectFmsBlockHasPlan(FmsBlockReqVo reqVo) {
        List<FmsBlockResVo> fmsBlockResVos = fmsBlockMapper.selectFmsBlockHasPlan(reqVo);
        if (fmsBlockResVos.size() > 0) {
            String[] blockIds = new String[fmsBlockResVos.size()];
            for (int i = 0; i < fmsBlockResVos.size(); i++) {
                blockIds[i] = fmsBlockResVos.get(i).getBlockId();
            }

            List<FmsBlockResVo> fmsBlockResVos1 = fmsCropMapper.selectFmsCropNameByBlockId(blockIds);

            for (FmsBlockResVo fmsBlockResVo : fmsBlockResVos) {
                for (FmsBlockResVo blockResVo : fmsBlockResVos1) {
                    if (fmsBlockResVo.getBlockId().equals(blockResVo.getBlockId())) {
                        fmsBlockResVo.setCropNames(blockResVo.getCropNames());
                        fmsBlockResVo.setCropPreviews(blockResVo.getCropPreviews());
                        break;
                    }
                }
            }
        }
        return fmsBlockResVos;
    }
}
