/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.pl.process.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.modules.pl.goods.entity.GoodsMaterialEntity;
import org.springblade.modules.pl.process.entity.ProcessEntity;
import org.springblade.modules.pl.process.entity.ProcessItemEntity;
import org.springblade.modules.pl.process.service.IProcessItemService;
import org.springblade.modules.pl.process.vo.ProcessVO;
import org.springblade.modules.pl.process.excel.ProcessExcel;
import org.springblade.modules.pl.process.mapper.ProcessMapper;
import org.springblade.modules.pl.process.service.IProcessService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 工序表 服务实现类
 *
 * @author BladeX
 * @since 2024-04-21
 */
@Service
public class ProcessServiceImpl extends BaseServiceImpl<ProcessMapper, ProcessEntity> implements IProcessService {

	@Resource
	private IProcessItemService processItemService;

	@Override
	public IPage<ProcessVO> selectProcessPage(IPage<ProcessVO> page, ProcessVO process) {
		return page.setRecords(baseMapper.selectProcessPage(page, process));
	}


	@Override
	public List<ProcessExcel> exportProcess(Wrapper<ProcessEntity> queryWrapper) {
		List<ProcessExcel> processList = baseMapper.exportProcess(queryWrapper);
		//processList.forEach(process -> {
		//	process.setTypeName(DictCache.getValue(DictEnum.YES_NO, Process.getType()));
		//});
		return processList;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public R submit(ProcessVO processVO) {
		BladeUser user = AuthUtil.getUser();
		if(Objects.requireNonNull(user).getUserCategory()==2){
			processVO.setFactoryId(user.getFactoryId());
		}
		if(processVO==null){
			return R.fail("数据异常");
		}
		if(processVO.getIsRetrospect()==1){
			if(processVO.getDetails().size()==0){
				return R.fail("请填写追溯要求信息");
			}
		}
		if(processVO.getIsPhotograph()==0){
			processVO.setIsDownline(0);
		}
		this.saveOrUpdate(processVO);
		processItemService.update(Wrappers.<ProcessItemEntity>lambdaUpdate().eq(ProcessItemEntity::getProcessId,processVO.getId()).set(ProcessItemEntity::getStatus,0));
		if(processVO.getIsRetrospect()==1){
			for(ProcessItemEntity processItem : processVO.getDetails()){
				processItem.setProcessId(processVO.getId());
				processItem.setStatus(1);
			}
			processItemService.saveOrUpdateBatch(processVO.getDetails());
		}
		processItemService.remove(Wrappers.<ProcessItemEntity>lambdaUpdate().eq(ProcessItemEntity::getProcessId,processVO.getId()).eq(ProcessItemEntity::getStatus,0));
		return R.success("操作成功");
	}

}
