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

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.fms.domain.FmsCrop;
import com.yunfeiyun.agmp.fms.domain.FmsPeriod;
import com.yunfeiyun.agmp.fms.mapper.FmsPeriodMapper;
import com.yunfeiyun.agmp.fms.domain.reqvo.AddPeriodListReqVo;
import com.yunfeiyun.agmp.fms.service.IFmsPeriodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 作物周期Service业务层处理
 * 
 * @author 杨晓辉
 * @date 2023-05-16
 */
@Service
public class FmsPeriodServiceImpl implements IFmsPeriodService 
{
    @Autowired
    private FmsPeriodMapper fmsPeriodMapper;
    @Resource
    private SysResMapper sysResMapper;

    /**
     * 查询作物周期
     * 
     * @param periodId 作物周期主键
     * @return 作物周期
     */
    @Override
    public FmsPeriod selectFmsPeriodByPeriodId(String periodId)
    {
        return fmsPeriodMapper.selectFmsPeriodByPeriodId(periodId);
    }

    @Override
    public List<FmsPeriod> selectFmsPeriodByCropId(String cropId) {
        return fmsPeriodMapper.selectFmsPeriodByCropId(cropId);
    }

    @Override
    public List<FmsCrop> selectFmsCropHasPeriod() {
        return fmsPeriodMapper.selectFmsCropHasPeriod();
    }

    /**
     * 查询作物周期列表
     * 
     * @param fmsPeriod 作物周期
     * @return 作物周期
     */
    @Override
    public List<FmsPeriod> selectFmsPeriodList(FmsPeriod fmsPeriod)
    {
        return fmsPeriodMapper.selectFmsPeriodList(fmsPeriod);
    }

    /**
     * 新增作物周期
     * 
     * @param fmsPeriod 作物周期
     * @return 结果
     */
    @Override
    public int insertFmsPeriod(FmsPeriod fmsPeriod)
    {
        fmsPeriod.setPeriodId(fmsPeriod.getId());
        return fmsPeriodMapper.insertFmsPeriod(fmsPeriod);
    }

    @Transactional
    @Override
    public int insertFmsPeriodBatch(AddPeriodListReqVo reqVo) {

        //查询生命周期列表，删除过去的图片
        List<FmsPeriod> fmsPeriods = fmsPeriodMapper.selectFmsPeriodByCropId(reqVo.getCropId());
        List<SysRes> sysResList = new ArrayList<>();
        String[] periodIds = new String[fmsPeriods.size()];
        for (int i = 0; i < fmsPeriods.size(); i++) {
            FmsPeriod fmsPeriod = fmsPeriods.get(i);
            SysRes res = new SysRes();
            res.setResBusId(fmsPeriod.getPeriodId());
            List<SysRes> sysRes = sysResMapper.selectSysResList(res);
            sysResList.addAll(sysRes);
            periodIds[i] = fmsPeriod.getPeriodId();
        }
//        for (FmsPeriod fmsPeriod : fmsPeriods) {
//            SysRes res = new SysRes();
//            res.setResBusId(fmsPeriod.getPeriodId());
//            List<SysRes> sysRes = sysResMapper.selectSysResList(res);
//            sysResList.addAll(sysRes);
//            sysResMapper.deleteSysResbes(new String[]{fmsPeriod.getPeriodId()});
//        }
        //删除过去的生长周期，保存最新的
        fmsPeriodMapper.deleteFmsPeriodByCropId(reqVo.getCropId());

        if (reqVo.getList().size() > 0) {
            for (int i = 0; i < reqVo.getList().size(); i++) {
                FmsPeriod fmsPeriod = reqVo.getList().get(i);
                fmsPeriod.setCropId(reqVo.getCropId());
                fmsPeriod.setPeriodId(fmsPeriod.getId());
                fmsPeriod.setPeriodSeq(i + "");

                fmsPeriodMapper.insertFmsPeriod(fmsPeriod);

                String resIdsStr = reqVo.getList().get(i).getResIds();
                if (!StringUtils.isEmpty(resIdsStr)) {
                    String[] resIds = resIdsStr.split(",");
                    for (String resId : resIds) {
                        SysRes sysRes = sysResMapper.selectSysResByResId(resId);
                        sysRes.setResBusId(fmsPeriod.getPeriodId());
                        sysResMapper.updateSysRes(sysRes);
                    }
                }
            }
//            fmsPeriodMapper.insertFmsPeriodBatch(reqVo.getList());
        }

        //删除图片
        if (periodIds.length > 0) {
            sysResMapper.deleteSysResbes(periodIds);
        }
        for (SysRes sysRes : sysResList) {
            String resUrl = sysRes.getResUrl();
            File file = new File(resUrl);
            if (file.exists()) {
                file.delete();
            }
        }

        return 1;
    }

    /**
     * 修改作物周期
     * 
     * @param fmsPeriod 作物周期
     * @return 结果
     */
    @Override
    public int updateFmsPeriod(FmsPeriod fmsPeriod)
    {
        return fmsPeriodMapper.updateFmsPeriod(fmsPeriod);
    }

    /**
     * 批量删除作物周期
     * 
     * @param periodIds 需要删除的作物周期主键
     * @return 结果
     */
    @Override
    public int deleteFmsPeriodByPeriodIds(String[] periodIds)
    {
        return fmsPeriodMapper.deleteFmsPeriodByPeriodIds(periodIds);
    }

    /**
     * 删除作物周期信息
     * 
     * @param periodId 作物周期主键
     * @return 结果
     */
    @Override
    public int deleteFmsPeriodByPeriodId(String periodId)
    {
        return fmsPeriodMapper.deleteFmsPeriodByPeriodId(periodId);
    }

    @Override
    public int deleteFmsPeriodByCropId(String cropId) {
        return fmsPeriodMapper.deleteFmsPeriodByCropId(cropId);
    }
}
