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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.yunfeiyun.agmp.common.constant.ErrorCode;
import com.yunfeiyun.agmp.common.constant.ResConstants;
import com.yunfeiyun.agmp.common.core.domain.AjaxResult;
import com.yunfeiyun.agmp.common.utils.DateUtils;
import com.yunfeiyun.agmp.common.utils.SecurityUtils;
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.MppQualitycheck;
import com.yunfeiyun.agmp.fms.mapper.MppQualitycheckMapper;
import com.yunfeiyun.agmp.fms.domain.reqvo.AddMppQualitycheckReqVo;
import com.yunfeiyun.agmp.fms.service.IMppQualitycheckService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


/**
 *  质检Service业务层处理
 * 
 * @author 杨晓辉
 * @date 2023-05-30
 */
@Service
public class MppQualitycheckServiceImpl implements IMppQualitycheckService
{
    @Autowired
    private MppQualitycheckMapper mppQualitycheckMapper;
    @Autowired
    private SysResMapper sysResMapper;
    @Resource
    private ISysResService sysResService;

    /**
     * 查询 质检
     * 
     * @param qualitycheckId  质检主键
     * @return  质检
     */
    @Override
    public MppQualitycheck selectMppQualitycheckByQualitycheckId(String qualitycheckId)
    {
        MppQualitycheck mppQualitycheck = mppQualitycheckMapper.selectMppQualitycheckByQualitycheckId(qualitycheckId);
        if (mppQualitycheck != null) {
            //查询图片资源
            List<SysRes> res = sysResMapper.selectResByBusId(qualitycheckId, ResConstants.BizType.MPPQUALITYCHECK.getKey(), null);
            String previews = "";
            String resIds = "";
            for (SysRes re : res) {
                previews += re.getResUrl() + ",";
                resIds += re.getResId() + ",";
            }
            if (previews.length() > 0) {
                previews = previews.substring(0, previews.length() - 1);
                resIds = resIds.substring(0, resIds.length() - 1);
            }
            mppQualitycheck.setQualitycheckPreviews(previews);
            mppQualitycheck.setQualitycheckResIds(resIds);
        }
        return mppQualitycheck;
    }

    /**
     * 查询 质检列表
     * 
     * @param mppQualitycheck  质检
     * @return  质检
     */
    @Override
    public List<MppQualitycheck> selectMppQualitycheckList(MppQualitycheck mppQualitycheck)
    {
        return mppQualitycheckMapper.selectMppQualitycheckList(mppQualitycheck);
    }

    /**
     * 新增 质检
     * 
     * @param reqVo  质检
     * @return 结果
     */
    @Transactional
    @Override
    public AjaxResult insertMppQualitycheck(AddMppQualitycheckReqVo reqVo)
    {
        String finishgoodsIds = reqVo.getFinishgoodsIds();
        if (StringUtils.isEmpty(finishgoodsIds)) {
            return AjaxResult.error(ErrorCode.FWMS_FINISH_GOODS_NOT_EXIST.getCode(), ErrorCode.FWMS_FINISH_GOODS_NOT_EXIST.getMessage());
        }
        String[] fgIds = finishgoodsIds.split(",");
        String userId = SecurityUtils.getLoginUser().getUserId();
        List<MppQualitycheck> addList = new ArrayList<>();

        String qualityResIds = reqVo.getQualityResIds();
        String[] resIds = qualityResIds.split(",");
        List<SysRes> res = sysResMapper.selectResByResIdes(resIds);
        List<SysRes> addResList = new ArrayList<>();

        for (int i = 0; i < fgIds.length; i++) {
            MppQualitycheck mqcheck = new MppQualitycheck();
            BeanUtils.copyProperties(reqVo, mqcheck);
            mqcheck.setQualitycheckId(mqcheck.getId());
            mqcheck.setFinishgoodsId(fgIds[i]);
            mqcheck.setQualitycheckCreator(userId);
            mqcheck.setQualitycheckCreateddate(DateUtils.dateTimeNow());
            addList.add(mqcheck);

            for (SysRes re : res) {
                if (i == 0) {
                    re.setResBusId(mqcheck.getQualitycheckId());

                } else {
                    SysRes r = new SysRes();
                    BeanUtils.copyProperties(re, r);
                    r.setResId(r.getId());
                    r.setResBusId(mqcheck.getQualitycheckId());
                    addResList.add(r);
                }
            }
        }
        mppQualitycheckMapper.insertMppQualitycheckBatch(addList);

        if (res.size() > 0) {
            sysResMapper.updateSysResByResIds(res);
        }
//        if (addResList.size() > 0) {
//            sysResMapper.insertSysReses(addResList);
//        }
        for (SysRes sysRes : addResList) {
            sysResMapper.insertSysRes(sysRes);
        }

        return AjaxResult.success();
    }

    /**
     * 修改 质检
     * 
     * @param mppQualitycheck  质检
     * @return 结果
     */
    @Override
    public int updateMppQualitycheck(MppQualitycheck mppQualitycheck)
    {
        SysRes sysRes = new SysRes();
        sysRes.setResBusId(mppQualitycheck.getQualitycheckId());
        List<SysRes> sysResList = sysResMapper.selectSysResList(sysRes);
        List<String> resIdsOld = new ArrayList<>();
        for (SysRes res : sysResList) {
            resIdsOld.add(res.getResId());
        }
        String resIdsStr = mppQualitycheck.getQualitycheckResIds();
        List<String> resIdsNew = new ArrayList<>();
        if (!StringUtils.isEmpty(resIdsStr)) {
            String[] resIds = resIdsStr.split(",");
            resIdsNew = Arrays.asList(resIds);
        }
        sysResService.updateSysRes(resIdsOld, resIdsNew, mppQualitycheck.getQualitycheckId());
        return mppQualitycheckMapper.updateMppQualitycheck(mppQualitycheck);
    }

    /**
     * 批量删除 质检
     * 
     * @param qualitycheckIds 需要删除的 质检主键
     * @return 结果
     */
    @Override
    public int deleteMppQualitycheckByQualitycheckIds(String[] qualitycheckIds)
    {
        return mppQualitycheckMapper.deleteMppQualitycheckByQualitycheckIds(qualitycheckIds);
    }

    /**
     * 删除 质检信息
     * 
     * @param qualitycheckId  质检主键
     * @return 结果
     */
    @Override
    public int deleteMppQualitycheckByQualitycheckId(String qualitycheckId)
    {
        return mppQualitycheckMapper.deleteMppQualitycheckByQualitycheckId(qualitycheckId);
    }
}
