package com.yh.saas.plugin.yiliangyiyun.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.yh.saas.common.support.util.IdGenerator;
import com.yh.saas.plugin.yiliangyiyun.constant.StatusEnum;
import com.yh.saas.plugin.yiliangyiyun.entity.FillingDetailInfo;
import com.yh.saas.plugin.yiliangyiyun.entity.FuelFillingInfo;
import com.yh.saas.plugin.yiliangyiyun.entity.TranProcessInfo;
import com.yh.saas.plugin.yiliangyiyun.mapper.FillingDetailInfoMapper;
import com.yh.saas.plugin.yiliangyiyun.service.IFillingDetailInfoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yh.saas.plugin.yiliangyiyun.service.IFuelFillingInfoService;
import jdk.net.SocketFlow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 记录加油明细 服务实现类
 * </p>
 *
 * @author Gdc
 * @since 2022-06-24
 */
@Service
public class FillingDetailInfoServiceImpl extends ServiceImpl<FillingDetailInfoMapper, FillingDetailInfo> implements IFillingDetailInfoService {

    @Autowired
    private IFuelFillingInfoService fuelFillingInfoService;
    /**
     * 添加明细
     * @param fillingDetailInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addFillingDetail(FillingDetailInfo fillingDetailInfo) {
        List<FillingDetailInfo> fillingDetailInfoList = fillingDetailInfo.getFillingDetailInfoList();
        if (!CollectionUtils.isEmpty(fillingDetailInfoList)) {
            for (FillingDetailInfo fillingDetailInfo1 : fillingDetailInfoList) {
                //新增主键id
                fillingDetailInfo1.setId(IdGenerator.generateUUID());
                //操作数据
                this.insert(fillingDetailInfo1);
            }
        }
        //查询加油信息
        FuelFillingInfo fuelFillingInfo = fuelFillingInfoService.selectOne(new EntityWrapper<FuelFillingInfo>()
                    .eq("id",fillingDetailInfo.getFillingDetailInfoList().get(0).getFillingId())
                    .eq("delete_flag","0"));
        if(fuelFillingInfo != null){
            fuelFillingInfo.setTfc(fillingDetailInfo.getTfc());
            fuelFillingInfo.setTotalCost(fillingDetailInfo.getTotalCost());
            //状态改为已申请
            fuelFillingInfo.setStatusFlag(StatusEnum.APPLY_ING.getFlag());
            fuelFillingInfo.setStatus(StatusEnum.APPLY_ING.getName());
            //更新主表数据
            fuelFillingInfoService.updateById(fuelFillingInfo);
        }
        return "OK";
    }
}
