/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.stocks.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.common.service.IFileInfoService;
import com.tofly.stocks.dto.CheckTaskInfoDto;
import com.tofly.stocks.dto.CheckTaskQuery;
import com.tofly.stocks.entity.CheckDetail;
import com.tofly.stocks.entity.CheckTask;
import com.tofly.stocks.entity.StockInfo;
import com.tofly.stocks.service.CheckDetailService;
import com.tofly.stocks.service.CheckTaskService;
import com.tofly.stocks.service.StockInfoService;
import com.tofly.stocks.vo.CheckDetailVo;
import com.tofly.stocks.vo.CheckTaskVo;
import java.util.List;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import com.tofly.stocks.mapper.CheckTaskMapper;

/**
 * 库存盘点任务表
 *
 * @author admin
 * @date Fri Nov 25 00:00:00 CST 2022
 */
@Service("checkTaskService")
public class CheckTaskServiceImpl extends ServiceImpl<CheckTaskMapper, CheckTask> implements CheckTaskService {
		
    @Autowired
    private IFileInfoService fileInfoService;
    @Autowired
    private CheckDetailService checkDetailService;        
    @Autowired
    private StockInfoService stockInfoService;        
    @Value("${flow.ftp.stocks.checkTask}")
    private String path;
	
	/**
	 * 根据盘点盘点数量改变库存数量
	 * @param checkDetail
	 */
	private void configStockInfo(CheckDetail checkDetail){		
		StockInfo existStockInfo = stockInfoService.getById(checkDetail.getStockInfoId());
		if(!checkDetail.getCheckNum().equals(existStockInfo.getStockNum())) {
			 StockInfo stockInfo=new StockInfo();
			 stockInfo.setId(existStockInfo.getId());
             stockInfo.setStockNum(checkDetail.getCheckNum());
             stockInfoService.updateById(stockInfo);			 
		}					
	}


	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean editCheckTaskInfo(CheckTaskInfoDto checkTaskInfoDto, List<MultipartFile> files) throws Exception {
		boolean flag;

		if (CollectionUtils.isEmpty(checkTaskInfoDto.getCheckDetails())) {
			throw new RuntimeException("使用明细单数据不能为空");
		}

		CheckTask checkTask = this.getById(checkTaskInfoDto.getId());
        if (checkTask == null) {
            throw new RuntimeException("修改，参数Id错误");
        }
        if (checkTask.getCheckStatus()==2) {
            throw new RuntimeException("已盘点确认数据不能修改");
        }

        
        CheckTask data = new CheckTask();
        BeanUtils.copyProperties(data,checkTaskInfoDto);

        data.setAnnex(fileInfoService.uploadFiles(files,data.getAnnex()
                ,checkTask.getAnnex(),path,"TF_STOCKS_CHECK_TASK"));

        flag = this.updateById(data);
        
        checkDetailService.remove(Wrappers.<CheckDetail>lambdaQuery()
				.eq(CheckDetail::getCheckTaskId,data.getId()));


		for (CheckDetail checkDetail : checkTaskInfoDto.getCheckDetails()) {
			StockInfo existStockInfo = stockInfoService.getById(checkDetail.getStockInfoId());
            checkDetail.setCheckStockNum(existStockInfo.getStockNum());
			String CheckResult="0";
			String CheckResultStatus="正常";
			Double result=checkDetail.getCheckNum()-existStockInfo.getStockNum();
			if(result>0){
				CheckResult="+"+result;
				CheckResultStatus="盘盈";
			} else if(result<0){
				CheckResult=""+result;
				CheckResultStatus="盘亏";
			}
			checkDetail.setCheckResult(CheckResult);
			checkDetail.setCheckStatus(CheckResultStatus);
			checkDetail.setCheckTaskId(data.getId());
            checkDetailService.save(checkDetail);
        }
        if(data.getCheckStatus()==2){ 
        	for (CheckDetail checkDetail : checkTaskInfoDto.getCheckDetails()){
        		configStockInfo(checkDetail);
        	}
        }

        return flag;
	}


	@Override
	public boolean deleteByIds(String ids) {
		// TODO Auto-generated method stub
		return false;
	}


	@Override
	public Page<CheckTaskVo> getDataPage(Page page, CheckTaskQuery checkTaskQuery) {
		return page.setRecords(baseMapper.getDataPage(page, checkTaskQuery));
	}

	@Override
	public List<CheckDetailVo> getCheckDetailsByTaskId(Long checkTaskId) {		
		return baseMapper.getCheckDetailsByTaskId(checkTaskId);
	}

	@Override
	public CheckTaskVo getByPlanTaskId(Long id) {
		Page<CheckTask> page=new Page<>();
		CheckTaskQuery checkTaskQuery=new CheckTaskQuery();
		checkTaskQuery.setId(id);
		page.setPages(1);
		page.setSize(1);
		return this.getDataPage(page,checkTaskQuery).getRecords().get(0);
	}


}
