package com.op.mes.service.impl;

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

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.domain.ProRoute;
import com.op.mes.mapper.BaseFileMapper;
import com.op.mes.mapper.ProRouteMapper;
import com.op.mes.service.IProRouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;


/**
 * 工艺路线Service业务层处理
 *
 * @author yinjinlu
 * @date 2022-05-12
 */
@Service
public class ProRouteServiceImpl implements IProRouteService
{
    @Autowired
    private ProRouteMapper proRouteMapper;
    @Autowired
    private BaseFileMapper baseFileMapper;
    /**
     * 查询工艺路线
     *
     * @param routeId 工艺路线主键
     * @return 工艺路线
     */
    @Override
    @DS("#header.poolName")
    public ProRoute selectProRouteByRouteId(String routeId)
    {

        ProRoute p = proRouteMapper.selectProRouteByRouteId(routeId);
        List<BaseFile> files =  baseFileMapper.getBaseFileBatch(routeId);
        if(!CollectionUtils.isEmpty(files)){
            p.setFiles(files);
        }
        return p;
    }

    /**
     * 查询工艺路线列表
     *
     * @param proRoute 工艺路线
     * @return 工艺路线
     */
    @Override
    @DS("#header.poolName")
    public List<ProRoute> selectProRouteList(ProRoute proRoute)
    {
        return proRouteMapper.selectProRouteList(proRoute);
    }

    @Override
    public ProRoute getRouteByProductId(String itemId) {
        return proRouteMapper.getRouteByProductId(itemId);
    }


    @Override
    @DS("#header.poolName")
    public Boolean checkRouteCodeUnique(ProRoute proRoute) {
        ProRoute route = proRouteMapper.checkRouteCodeUnique(proRoute);
        String routeId = proRoute.getRouteId();
        if(StringUtils.isNotNull(route) && !route.getRouteId().equals(routeId)){
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 新增工艺路线
     *
     * @param proRoute 工艺路线
     * @return 结果
     */
    @Override
    @DS("#header.poolName")
    public int insertProRoute(ProRoute proRoute)
    {
        proRoute.setCreateTime(DateUtils.getNowDate());
        proRoute.setRouteVersion("V.1");
        //上传附件
        if(StringUtils.isNotEmpty(proRoute.getFileList())){
            String[] ids = proRoute.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(proRoute.getRouteId());
                file.setCreateBy(SecurityUtils.getUsername());
                file.setCreateTime(new Date());
                files.add(file);
            }
            baseFileMapper.insertBaseFileBatch(files);
        }

        return proRouteMapper.insertProRoute(proRoute);
    }

    /**
     * 修改工艺路线
     *
     * @param proRoute 工艺路线
     * @return 结果
     */
    @Override
    @DS("#header.poolName")
    public int updateProRoute(ProRoute proRoute)
    {
        proRoute.setUpdateTime(DateUtils.getNowDate());

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

        //更新版本V.1->递增
        String nowRouteVersion = proRoute.getRouteVersion().replace("V","")
                                                           .replace(".","");
        int nowRouteVersionNum = Integer.parseInt(nowRouteVersion);
        String newRouteVersionNum = "V."+(++nowRouteVersionNum);
        proRoute.setRouteVersion(newRouteVersionNum);
        return proRouteMapper.updateProRoute(proRoute);
    }

    /**
     * 批量删除工艺路线
     *
     * @param routeIds 需要删除的工艺路线主键
     * @return 结果
     */
    @Override
    @DS("#header.poolName")
    public int deleteProRouteByRouteIds(String[] routeIds)
    {
        return proRouteMapper.deleteProRouteByRouteIds(routeIds);
    }

    /**
     * 删除工艺路线信息
     *
     * @param routeId 工艺路线主键
     * @return 结果
     */
    @Override
    @DS("#header.poolName")
    public int deleteProRouteByRouteId(String routeId)
    {
        return proRouteMapper.deleteProRouteByRouteId(routeId);
    }

}
