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

import com.yunfeiyun.agmp.common.constant.ErrorCode;
import com.yunfeiyun.agmp.common.enums.MachStatus;
import com.yunfeiyun.agmp.common.enums.TaskrcdAuditStatus;
import com.yunfeiyun.agmp.common.exception.BizException;
import com.yunfeiyun.agmp.common.utils.DateUtils;
import com.yunfeiyun.agmp.fms.domain.FmsMach;
import com.yunfeiyun.agmp.fms.mapper.FmsMachMapper;
import com.yunfeiyun.agmp.fms.domain.reqvo.FmsMachListReqVo;
import com.yunfeiyun.agmp.fms.domain.reqvo.FmsMachRecordListReqVo;
import com.yunfeiyun.agmp.fms.domain.resvo.FmsMachAppMapResVo;
import com.yunfeiyun.agmp.fms.domain.resvo.FmsMachRecordListResVo;
import com.yunfeiyun.agmp.fms.service.IFmsMachService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.*;

/**
 * 机具Service业务层处理
 * 
 * @author 杨晓辉
 * @date 2023-05-25
 */
@Service
public class FmsMachServiceImpl implements IFmsMachService 
{
    @Autowired
    private FmsMachMapper fmsMachMapper;

    /**
     * 查询机具
     * 
     * @param machId 机具主键
     * @return 机具
     */
    @Override
    public FmsMach selectFmsMachByMachId(String machId)
    {
        return fmsMachMapper.selectFmsMachByMachId(machId);
    }

    /**
     * 查询机具列表
     * 
     * @param fmsMachListReqVo 机具
     * @return 机具
     */
    @Override
    public List<FmsMach> selectFmsMachList(FmsMachListReqVo fmsMachListReqVo)
    {
        return fmsMachMapper.selectFmsMachList(fmsMachListReqVo);
    }

    /**
     * 查询机具列表
     * @return 机具集合
     */
    @Override
    public List<FmsMachAppMapResVo> selectFmsMachAppMapResVoList(){
        List<FmsMach> fmsMachList = fmsMachMapper.selectFmsMachList(null);
        Map<String, FmsMachAppMapResVo> map = new HashMap<>();
        for(FmsMach item : fmsMachList){
            FmsMachAppMapResVo vo = map.get(item.getMachType());
            if(null == vo){
                vo = new FmsMachAppMapResVo();
                vo.setId(item.getMachType());
                map.put(vo.getId(),vo);
            }
            vo.getContents().add(item);
        }
        return new ArrayList<>(map.values());
    }

    /**
     * 查询农机具使用记录列表
     *
     * @param fmsMachRecordListReqVo 机具
     * @return 机具集合
     */
    @Override
    public List<FmsMachRecordListResVo> selectFmsMachRecordList(FmsMachRecordListReqVo fmsMachRecordListReqVo)
    {
        fmsMachRecordListReqVo.setTaskrcdAuditstatus(TaskrcdAuditStatus.PASS.getCode());
        String startDate = null;
        String endDate = null;
        try{
            startDate = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, DateUtils.parseDate(fmsMachRecordListReqVo.getStartDate()));
        } catch (Exception ignored){
            // 不处理
        }

        try{
            endDate = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, DateUtils.parseDate(fmsMachRecordListReqVo.getEndDate()));
        } catch (Exception ignored){
            // 不处理
        }

        fmsMachRecordListReqVo.setStartDate(startDate);
        fmsMachRecordListReqVo.setEndDate(endDate);
        return fmsMachMapper.selectFmsMachRecordList(fmsMachRecordListReqVo);
    }


    /**
     * 新增机具
     * 
     * @param fmsMach 机具
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertFmsMach(FmsMach fmsMach)
    {
        String machNo = fmsMach.getMachNo();
        FmsMach oldFmsMach = this.fmsMachMapper.selectFmsMachByMachNo(machNo);
        if (!ObjectUtils.isEmpty(oldFmsMach)){
            throw new BizException(ErrorCode.FMS_MACH_RECORD_EXIST);
        }
        fmsMach.setMachId(fmsMach.getId());
        fmsMach.setMachUsestatus(MachStatus.UNUSED.getCode());
        return fmsMachMapper.insertFmsMach(fmsMach);
    }

    /**
     * 修改机具
     * 
     * @param fmsMach 机具
     * @return 结果
     */
    @Override
    public int updateFmsMach(FmsMach fmsMach)
    {
        String machId = fmsMach.getMachId();
        String machNo = fmsMach.getMachNo();
        FmsMach oldFmsMach = this.fmsMachMapper.selectFmsMachByMachId(machId);
        if (ObjectUtils.isEmpty(oldFmsMach)){
            throw new BizException(ErrorCode.FMS_MACH_RECORD_NO_EXIST);
        }
        FmsMach tempFmsMach = this.fmsMachMapper.selectFmsMachByMachNo(machNo);
        if (!ObjectUtils.isEmpty(tempFmsMach) && !Objects.equals(machId, tempFmsMach.getMachId()) &&
                Objects.equals(machNo, tempFmsMach.getMachNo())){
            throw new BizException(ErrorCode.FMS_MACH_RECORD_MACHNO_EXIST);
        }
        fmsMach.setMachUsestatus(null);
        return fmsMachMapper.updateFmsMach(fmsMach);
    }

    /**
     * 批量删除机具
     * 
     * @param machIds 需要删除的机具主键
     * @return 结果
     */
    @Override
    public int deleteFmsMachByMachIds(String[] machIds)
    {
        return fmsMachMapper.deleteFmsMachByMachIds(machIds);
    }

    /**
     * 删除机具信息
     * 
     * @param machId 机具主键
     * @return 结果
     */
    @Override
    public int deleteFmsMachByMachId(String machId)
    {
        FmsMach oldFmsMach = this.fmsMachMapper.selectFmsMachByMachId(machId);
        if (ObjectUtils.isEmpty(oldFmsMach)){
            throw new BizException(ErrorCode.FMS_MACH_RECORD_NO_EXIST);
        }
        return fmsMachMapper.deleteFmsMachByMachId(machId);
    }
}
