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.ProProcess;
import com.op.mes.mapper.BaseFileMapper;
import com.op.mes.mapper.ProProcessMapper;
import com.op.mes.service.IProProcessService;

import com.op.system.api.RemoteUserService;
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 ProProcessServiceImpl implements IProProcessService {
    @Autowired
    private ProProcessMapper proProcessMapper;

    @Autowired
    private BaseFileMapper baseFileMapper;

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

    /**
     * 查询生产工序列表
     *
     * @param proProcess 生产工序
     * @return 生产工序
     */
    @Override
    @DS("#header.poolName")
    public List<ProProcess> selectProProcessList(ProProcess proProcess) {
        return proProcessMapper.selectProProcessList(proProcess);
    }

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


    /**
     * 检查工序名称是否唯一
     *
     * @param proProcess
     * @return
     */
    @Override
    @DS("#header.poolName")
    public Boolean checkProcessNameUnique(ProProcess proProcess) {
        ProProcess process = proProcessMapper.checkProcessNameUnique(proProcess);

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

    /**
     * 新增生产工序
     *
     * @param proProcess 生产工序
     * @return 结果
     */
    @Override
    @DS("#header.poolName")
    public int insertProProcess(ProProcess proProcess) {
        proProcess.setProcessId(IdUtils.fastSimpleUUID());
        proProcess.setCreateTime(DateUtils.getNowDate());

        //上传附件
        if (StringUtils.isNotEmpty(proProcess.getFileList())) {
            String[] ids = proProcess.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(proProcess.getProcessId());
                file.setCreateBy(SecurityUtils.getUsername());
                file.setCreateTime(new Date());
                files.add(file);
            }
            baseFileMapper.insertBaseFileBatch(files);
        }

        return proProcessMapper.insertProProcess(proProcess);
    }

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

        //上传附件
        if (StringUtils.isNotEmpty(proProcess.getFileList())) {
            baseFileMapper.deleteBaseFileBySourceId(proProcess.getProcessId());
            String[] ids = proProcess.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(proProcess.getProcessId());
                file.setCreateBy(SecurityUtils.getUsername());
                file.setCreateTime(new Date());
                files.add(file);
            }
            baseFileMapper.insertBaseFileBatch(files);
        }

        proProcess.setUpdateTime(DateUtils.getNowDate());
        return proProcessMapper.updateProProcess(proProcess);
    }

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

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