package com.op.mes.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.op.common.core.constant.UserConstants;
import com.op.common.core.domain.BaseFileData;
import com.op.common.core.utils.DateUtils;
import com.op.common.core.utils.StringUtils;
import com.op.common.core.utils.uuid.IdUtils;
import com.op.common.security.utils.SecurityUtils;
import com.op.mes.domain.BaseFile;
import com.op.mes.domain.ProRecipe;
import com.op.mes.mapper.BaseFileMapper;
import com.op.mes.mapper.ProRecipeMapper;
import com.op.mes.service.IProRecipeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 生产工序Service业务层处理
 *
 * @author yinjinlu
 * @date 2022-05-11
 */
@Service
public class ProRecipeServiceImpl implements IProRecipeService {
    @Autowired
    private ProRecipeMapper proRecipeMapper;

    @Autowired
    private BaseFileMapper baseFileMapper;

    /**
     * 查询生产工序
     *
     * @param processId 生产工序主键
     * @return 生产工序
     */
    @Override
    @DS("#header.poolName")
    public ProRecipe selectProRecipeByRecipeId(String processId) {
        ProRecipe p = proRecipeMapper.selectProRecipeByRecipeId(processId);
        List<BaseFile> files = baseFileMapper.getBaseFileBatch(processId);
        if (!CollectionUtils.isEmpty(files)) {
            p.setFiles(files);
        }
        return p;
    }

    /**
     * 查询生产工序列表
     *
     * @param proRecipe 生产工序
     * @return 生产工序
     */
    @Override
    @DS("#header.poolName")
    public List<ProRecipe> selectProRecipeList(ProRecipe proRecipe) {
        return proRecipeMapper.selectProRecipeList(proRecipe);
    }

    /**
     * 检查工序编码是否唯一
     *
     * @param proRecipe
     * @return
     */
    @Override
    @DS("#header.poolName")
    public Boolean checkRecipeCodeUnique(ProRecipe proRecipe) {
        ProRecipe process = proRecipeMapper.checkRecipeCodeUnique(proRecipe);
        if (StringUtils.isNotNull(process) && !process.getRecipeId().equals(proRecipe.getRecipeId())) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }


    /**
     * 检查工序名称是否唯一
     *
     * @param proRecipe
     * @return
     */
    @Override
    @DS("#header.poolName")
    public Boolean checkRecipeNameUnique(ProRecipe proRecipe) {
        ProRecipe process = proRecipeMapper.checkRecipeNameUnique(proRecipe);

        if (StringUtils.isNotNull(process) && !process.getRecipeId().equals(proRecipe.getRecipeId())) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 新增生产工序
     *
     * @param proRecipe 生产工序
     * @return 结果
     */
    @Override
    @DS("#header.poolName")
    public int insertProRecipe(ProRecipe proRecipe) {
        proRecipe.setRecipeId(IdUtils.fastSimpleUUID());
        proRecipe.setCreateTime(DateUtils.getNowDate());

        //上传附件
        if (StringUtils.isNotEmpty(proRecipe.getFileList())) {
            String[] ids = proRecipe.getFileList().split(",");
            List<BaseFileData> files = new ArrayList<>();
            BaseFileData file = null;
            for (String id : ids) {
                file = new BaseFileData();
                file.setFileId(IdUtils.fastSimpleUUID());
                file.setFileName(id.split("&fileName=")[1]);
                file.setFileAddress(id);
                file.setSourceId(proRecipe.getRecipeId());
                file.setCreateBy(SecurityUtils.getUsername());
                file.setCreateTime(new Date());
                files.add(file);
            }
            baseFileMapper.insertBaseFileBatch(files);
        }

        return proRecipeMapper.insertProRecipe(proRecipe);
    }

    /**
     * 修改生产工序
     *
     * @param proRecipe 生产工序
     * @return 结果
     */
    @Override
    @DS("#header.poolName")
    public int updateProRecipe(ProRecipe proRecipe) {

        //上传附件
        if (StringUtils.isNotEmpty(proRecipe.getFileList())) {
            baseFileMapper.deleteBaseFileBySourceId(proRecipe.getRecipeId());
            String[] ids = proRecipe.getFileList().split(",");
            List<BaseFileData> files = new ArrayList<>();
            BaseFileData file = null;
            for (String id : ids) {
                file = new BaseFileData();
                file.setFileId(IdUtils.fastSimpleUUID());
                file.setFileAddress(id);
                file.setFileName(id.split("&fileName=")[1]);
                file.setSourceId(proRecipe.getRecipeId());
                file.setCreateBy(SecurityUtils.getUsername());
                file.setCreateTime(new Date());
                files.add(file);
            }
            baseFileMapper.insertBaseFileBatch(files);
        }

        proRecipe.setUpdateTime(DateUtils.getNowDate());
        return proRecipeMapper.updateProRecipe(proRecipe);
    }

    /**
     * 批量删除生产工序
     *
     * @param processIds 需要删除的生产工序主键
     * @return 结果
     */
    @Override
    @DS("#header.poolName")
    public int deleteProRecipeByRecipeIds(String[] processIds) {
        return proRecipeMapper.deleteProRecipeByRecipeIds(processIds);
    }

    /**
     * 删除生产工序信息
     *
     * @param processId 生产工序主键
     * @return 结果
     */
    @Override
    @DS("#header.poolName")
    public int deleteProRecipeByRecipeId(String processId) {
        return proRecipeMapper.deleteProRecipeByRecipeId(processId);
    }
}
