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

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.yh.saas.common.support.util.IdGenerator;
import com.yh.saas.plugin.yiliangyiyun.constant.StatusEnum;
import com.yh.saas.plugin.yiliangyiyun.entity.FuelFillingInfo;
import com.yh.saas.plugin.yiliangyiyun.entity.ShortFillingInfo;
import com.yh.saas.plugin.yiliangyiyun.mapper.ShortFillingInfoMapper;
import com.yh.saas.plugin.yiliangyiyun.service.IFuelFillingInfoService;
import com.yh.saas.plugin.yiliangyiyun.service.IShortFillingInfoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 记录短途加油信息 服务实现类
 * </p>
 *
 * @author Gdc
 * @since 2022-07-07
 */
@Service
public class ShortFillingInfoServiceImpl extends ServiceImpl<ShortFillingInfoMapper, ShortFillingInfo> implements IShortFillingInfoService {

    @Autowired
    private IShortFillingInfoService shortFillingInfoService;
    @Autowired
    private IFuelFillingInfoService fuelFillingInfoService;

    /**
     * 添加短途加油信息
     *
     * @param shortFillingInfo
     * @return
     */
    @Override
    public String addShortFilling(ShortFillingInfo shortFillingInfo) {
        String id = "";
        //判断开始/结束轨迹(0开始1结束)
        if ("0".equals(shortFillingInfo.getTrackFlag())) {
            //新增主键id
            shortFillingInfo.setId(IdGenerator.generateUUID());
            //行程状态改为已开始
            shortFillingInfo.setTravelStatus(StatusEnum.TRACK_START.getName());
            shortFillingInfo.setTravelStatusFlag(StatusEnum.TRACK_START.getFlag());
            this.insert(shortFillingInfo);
            id = shortFillingInfo.getId();
        } else {
            ShortFillingInfo shortFillingInfo1 = shortFillingInfoService.selectOne(new EntityWrapper<ShortFillingInfo>()
                    .eq("comp_id", shortFillingInfo.getCompId())
                    .eq("common_id", shortFillingInfo.getCommonId())
                    .eq("car_no", shortFillingInfo.getCarNo()));
            if (shortFillingInfo1 != null) {
                //行程状态改为已结束
                shortFillingInfo1.setTravelStatusFlag(StatusEnum.TRACK_END.getFlag());
                shortFillingInfo1.setTravelStatus(StatusEnum.TRACK_END.getName());
                shortFillingInfo1.setDestinationProvince(shortFillingInfo.getDestinationProvince());
                shortFillingInfo1.setDestinationCity(shortFillingInfo.getDestinationCity());
                shortFillingInfo1.setDestinationArea(shortFillingInfo.getDestinationArea());
                shortFillingInfo1.setReceiveWarehouse(shortFillingInfo.getReceiveWarehouse());
                shortFillingInfo1.setDestinationLongitude(shortFillingInfo.getDestinationLongitude());
                shortFillingInfo1.setDestinationLatitude(shortFillingInfo.getDestinationLatitude());
                shortFillingInfo1.setMileage(shortFillingInfo.getMileage());
                this.updateById(shortFillingInfo1);
                id = shortFillingInfo1.getId();
            }
        }
        return id;
    }

    /**
     * 短途加油信息查询
     *
     * @param shortFillingInfo
     * @return
     */
    @Override
    public Page<ShortFillingInfo> selectShortFilling(ShortFillingInfo shortFillingInfo) {
        Map<String, Object> pageView = new HashMap<>();
        pageView.put("startRecord", (shortFillingInfo.getCurrentPage() - 1)
                * shortFillingInfo.getPageSize());
        //  公司ID
        pageView.put("compId", shortFillingInfo.getCompId());
        pageView.put("commonId", shortFillingInfo.getCommonId());
        pageView.put("searchType", shortFillingInfo.getSearchType());
        pageView.put("pageSize", shortFillingInfo.getPageSize());
        pageView.put("currentPage", shortFillingInfo.getCurrentPage());
        // 查询总数
        Integer dataCount = baseMapper.getCountByCondition(pageView);
        List<ShortFillingInfo> dataList = baseMapper.getListByCondition(pageView);
        Page<ShortFillingInfo> page = new Page<>();
        page.setRecords(dataList == null ? Lists.newArrayList() : dataList);
        page.setTotal(dataCount == null ? 0 : dataCount);
        page.setCurrent(shortFillingInfo.getCurrentPage());
        page.setSize(shortFillingInfo.getPageSize());
        return page;
    }

    /**
     * 查看短途加油信息
     *
     * @param id
     * @return
     */
    @Override
    public ShortFillingInfo getShortFilling(String id) {
        //根据id查询加油信息
        ShortFillingInfo shortFillingInfo = this.selectById(id);
        return shortFillingInfo;
    }

    /**
     * 短途请款
     *
     * @param shortFillingInfo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public String shortRequestFunds(ShortFillingInfo shortFillingInfo) {
        List<ShortFillingInfo> shortFillingInfoList = shortFillingInfo.getShortFillingInfoList();
        String id = IdGenerator.generateUUID();
        if (!CollectionUtils.isEmpty(shortFillingInfoList)) {
            for (ShortFillingInfo shortFillingInfo1 : shortFillingInfoList) {
                shortFillingInfo1.setFillingId(id);
                shortFillingInfo1.setWarehouseName(shortFillingInfo.getWarehouseName());
                this.updateById(shortFillingInfo1);
            }
        }
        FuelFillingInfo fuelFillingInfo = new FuelFillingInfo();
        //新增主表id
        fuelFillingInfo.setId(id);
        //定义总里程
        Double totalMileage = 0.0;
        for (int i = 0; i < shortFillingInfoList.size(); i++) {
            totalMileage = totalMileage + shortFillingInfoList.get(i).getMileage();
            fuelFillingInfo.setTotalMileage(totalMileage);
        }
        fuelFillingInfo.setCompId(shortFillingInfoList.get(0).getCompId());
        fuelFillingInfo.setStrokeType("3");
        //状态改为已申请
        fuelFillingInfo.setStatus(StatusEnum.APPLY_ING.getName());
        fuelFillingInfo.setStatusFlag(StatusEnum.APPLY_ING.getFlag());
        fuelFillingInfo.setWarehouseName(shortFillingInfo.getWarehouseName());
        fuelFillingInfoService.insert(fuelFillingInfo);
        return "OK";
    }
}
