package com.pj.project.tb_business_car;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pj.current.config.PartConfig;
import com.pj.project.tb_business.TbBusiness;
import com.pj.project.tb_business.TbBusinessService;
import com.pj.project.tb_business_item.TbBusinessItem;
import com.pj.project.tb_business_item.TbBusinessItemService;
import com.pj.utils.so.SoMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.pj.utils.sg.*;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Service: tb_business_car --
 *
 * @author qzy
 */
@Service
@Transactional
public class TbBusinessCarService extends ServiceImpl<TbBusinessCarMapper, TbBusinessCar> implements IService<TbBusinessCar> {

    /**
     * 底层 Mapper 对象
     */
    @Autowired
    TbBusinessCarMapper tbBusinessCarMapper;
    @Resource
    @Lazy
    private TbBusinessService tbBusinessService;
    @Resource
    PartConfig partConfig;


    /**
     * 查集合 - 根据条件（参数为空时代表忽略指定条件）
     */
    List<TbBusinessCar> getList(SoMap so) {
        return tbBusinessCarMapper.getList(so);
    }

    public TbBusinessCar findByBusinessIdAndCarNo(String businessId, String carNo) {
        QueryWrapper<TbBusinessCar> ew = new QueryWrapper<>();
        ew.eq("business_id", businessId)
                .eq("car_no", carNo.toUpperCase());
        return getOne(ew);
    }

    public TbBusinessCar findInAndNoBusinessCar(String carNo) {
        QueryWrapper<TbBusinessCar> ew = new QueryWrapper<>();
        ew.eq("car_no", carNo.toUpperCase()).isNotNull("real_in_time")
                .isNull("real_out_time");
        return getOne(ew);
    }

    public List<TbBusinessCar> findInAndNoBusinessCar() {
        QueryWrapper<TbBusinessCar> ew = new QueryWrapper<>();
        ew.isNotNull("real_in_time")
                .isNull("real_out_time");
        return list(ew);
    }

    public void removeByBusinessId(String businessId) {
        QueryWrapper<TbBusinessCar> ew = new QueryWrapper<>();
        ew.eq("business_id", businessId).isNull("real_in_time");
        remove(ew);
    }

    public List<TbBusinessCar> findByBusinessId(String businessId) {
        QueryWrapper<TbBusinessCar> ew = new QueryWrapper<>();
        ew.eq("business_id", businessId);
        return list(ew);
    }


    public List<TbBusinessCar> searchPartCar(String carNo) {
        QueryWrapper<TbBusinessCar> ew = new QueryWrapper<>();
        ew.like("car_no", carNo.toUpperCase()).and(j -> j.eq("pay", "0").or(i -> i.isNull("real_out_time").isNotNull("real_in_time")));
        return list(ew);
    }


    public TbBusinessCar findNotInCar(String carNo) {
        QueryWrapper<TbBusinessCar> ew = new QueryWrapper<>();
        ew.eq("car_no", carNo.toUpperCase()).isNull("real_in_time");
        return this.getOne(ew);
    }

    public TbBusinessCar findNotOutCar(String carNo) {
        QueryWrapper<TbBusinessCar> ew = new QueryWrapper<>();
        ew.eq("car_no", carNo.toUpperCase())
                .isNull("real_out_time")
                .isNotNull("real_in_time");
        return getOne(ew);
    }

    public TbBusinessCar check(String carNo) {
        QueryWrapper<TbBusinessCar> ew = new QueryWrapper<>();
        ew.eq("car_no", carNo.toUpperCase())
                .and(i -> i.isNull("real_in_time").or().isNull("real_out_time"));
        return getOne(ew);
    }

    public void add(TbBusinessCar t) throws Exception {
        String carNo = t.getCarNo();
        TbBusinessCar db = this.check(carNo);
        String businessId = t.getBusinessId();
        if (db != null) {
            throw new Exception("该车有未完成业务");
        }
        TbBusinessCar businessCar = this.findByBusinessIdAndCarNo(businessId, carNo);
        if (businessCar != null) {
            throw new Exception("车辆已存在");
        }
        TbBusiness tbBusiness = tbBusinessService.getById(businessId);
        if (tbBusiness!=null){
            tbBusiness.setChinaPartMoney(tbBusiness.getChinaPartMoney().add(partConfig.getBasePrice()));
            tbBusinessService.updateById(tbBusiness);
        }


        this.save(t);
    }

    public void updateCar(TbBusinessCar t) {
        this.updateById(t);
    }

    public void adminIn(String businessId, String carNo, Date realInTime) {
        TbBusinessCar car = this.findByBusinessIdAndCarNo(businessId, carNo);
        car.setRealInTime(realInTime);
        this.updateById(car);
    }

    public void adminOut(String businessId, String carNo, Date realOutTime) {
        TbBusinessCar car = this.findByBusinessIdAndCarNo(businessId, carNo);
        car.setRealOutTime(realOutTime);
        this.updateById(car);
    }

    public TbBusinessCar findInBuNoOutAndNotPay(String carNo) {
        QueryWrapper<TbBusinessCar> ew = new QueryWrapper<>();
        ew.eq("car_no", carNo.toUpperCase())
                .isNull("real_out_time")
                .isNotNull("real_in_time").eq("pay", "0");
        return getOne(ew);
    }

    public void deleteCar(String id) {
        TbBusinessCar db=this.getById(id);
        this.removeById(id);
        TbBusiness tbBusiness = tbBusinessService.getById(db.getBusinessId());
        if (tbBusiness!=null){
            tbBusiness.setChinaPartMoney(tbBusiness.getChinaPartMoney().subtract(partConfig.getBasePrice()));
            tbBusinessService.updateById(tbBusiness);
        }
    }
}
