package com.sckw.truck.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sckw.truck.entity.*;
import com.sckw.truck.entity.req.BusinessTruckPageListParam;
import com.sckw.truck.entity.req.BusinessTruckReq;
import com.sckw.truck.entity.res.BusinessTruckDetailRes;
import com.sckw.truck.entity.res.BusinessTruckPageListRes;
import com.sckw.truck.mapper.*;
import com.sckw.core.model.page.PageRes;
import com.sckw.core.web.response.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @desc:
 * @author: lt
 * @date: 2024/4/5 15:32
 */
@Service
public class BusinessTruckService{

    @Autowired
    KwBusinessTruckMapper kwBusinessTruckMapper;

    @Autowired
    KwBusinessTruckTransportLicenseMapper kwBusinessTruckTransportLicenseMapper;

    @Autowired
    kwBusinessTruckMoreMapper kwBusinessTruckMoreMapper;

    @Autowired
    KwBusinessTruckEnergyMapper kwBusinessTruckEnergyMapper;

    @Autowired
    KwBusinessTruckTypeMapper kwBusinessTruckTypeMapper;


    public HttpResult truckPageList(BusinessTruckPageListParam businessTruckPageListParam)
    {
        PageHelper.startPage(businessTruckPageListParam.getPage(), businessTruckPageListParam.getPageSize());
         List<BusinessTruckPageListRes> businessTruckPageListRes = kwBusinessTruckMapper.truckPageList(businessTruckPageListParam);
         businessTruckPageListRes.forEach(e -> {
             e.setStatusStr(e.getStatus() == 1 ? "繁忙" : "空闲");
         });
        return HttpResult.ok(new PageRes<>(new PageInfo<>(businessTruckPageListRes)));
    }

    /**
     * @desc: 车辆新增
     * @author: lt
     * @date: 2024/4/5 15:40
     */
    public HttpResult truckInsert(BusinessTruckReq businessTruckReq)
    {

        if (ObjectUtils.isNull(businessTruckReq)) {
            return HttpResult.error("数据不存在");
        }

        KwBusinessTruck businessTruckSource = kwBusinessTruckMapper
                .selectOne(new LambdaQueryWrapper<KwBusinessTruck>()
                        .eq(KwBusinessTruck::getTruckNo,businessTruckReq.getTruckNo())
                        .eq(KwBusinessTruck::getDelFlag, 0)
                );
        if (businessTruckSource != null) {
            throw new RuntimeException("当前车牌已存在");
        }


        KwBusinessTruck businessTruck = BeanUtil.copyProperties(businessTruckReq, KwBusinessTruck.class);
        int insert = kwBusinessTruckMapper.insert(businessTruck);
        if (insert > 0 ){
            Long truckId = businessTruck.getId();
            KwBusinessTruckTransportLicense kwBusinessTruckTransportLicense = BeanUtil.copyProperties(businessTruckReq.getTransportLicense(), KwBusinessTruckTransportLicense.class);
            kwBusinessTruckTransportLicense.setTruckId(truckId);
            kwBusinessTruckTransportLicenseMapper.insert(kwBusinessTruckTransportLicense);

            KwBusinessTruckMore kwBusinessTruckMore = BeanUtil.copyProperties(businessTruckReq.getKwBusinessTruckMoreReq(), KwBusinessTruckMore.class);
            kwBusinessTruckMore.setTruckId(truckId);
            kwBusinessTruckMoreMapper.insert(kwBusinessTruckMore);

        }
        return HttpResult.ok("新增成功");

    }

    /**
     * @desc: 车辆更新
     * @author: lt
     * @date: 2024/4/5 15:51
     */
    public HttpResult truckUpdate(BusinessTruckReq businessTruckReq)
    {
        //查找要修改的车辆是否存在
        KwBusinessTruck businessTruckSource = kwBusinessTruckMapper
                .selectOne(new LambdaQueryWrapper<KwBusinessTruck>()
                        .eq(KwBusinessTruck::getId,businessTruckReq.getId())
                        .eq(KwBusinessTruck::getDelFlag, 0)
                );
        if (ObjectUtils.isNull(businessTruckSource)){
            return HttpResult.error("当前车辆未找到");
        }

        //当前要修改的车牌不能和已存在车牌相同
        KwBusinessTruck businessTruckSource2 = kwBusinessTruckMapper
                .selectOne(new LambdaQueryWrapper<KwBusinessTruck>()
                        .ne(KwBusinessTruck::getId,businessTruckReq.getId())
                        .eq(KwBusinessTruck::getTruckNo, businessTruckReq.getTruckNo())
                        .eq(KwBusinessTruck::getDelFlag, 0)
                );
        if (ObjectUtils.isNull(businessTruckSource)){
            return HttpResult.error("当前车辆未找到");
        }
        if (ObjectUtils.isNotNull(businessTruckSource2)) {
            return HttpResult.error("当前车牌已存在");
        }

        KwBusinessTruck businessTruck = BeanUtil.copyProperties(businessTruckReq, KwBusinessTruck.class);
        final int i = kwBusinessTruckMapper.updateById(businessTruck);
        if (i > 0) {
            LambdaUpdateWrapper<KwBusinessTruckTransportLicense> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(KwBusinessTruckTransportLicense::getTruckId, businessTruck.getId());
            KwBusinessTruckTransportLicense kwBusinessTruckTransportLicenseReq = BeanUtil.copyProperties(businessTruckReq.getTransportLicense(), KwBusinessTruckTransportLicense.class);
            kwBusinessTruckTransportLicenseMapper.update(kwBusinessTruckTransportLicenseReq, wrapper);

            LambdaUpdateWrapper<KwBusinessTruckMore> wrapperTwo = new LambdaUpdateWrapper<>();
            wrapperTwo.eq(KwBusinessTruckMore::getTruckId, businessTruck.getId()).eq(KwBusinessTruckMore::getDelFlag, 0);
            KwBusinessTruckMore kwBusinessTruckMore = BeanUtil.copyProperties(businessTruckReq.getKwBusinessTruckMoreReq(), KwBusinessTruckMore.class);
            kwBusinessTruckMoreMapper.update(kwBusinessTruckMore, wrapperTwo);
        }
        return HttpResult.ok("编辑成功");
    }

    /**
     * @desc: 车辆删除
     * @author: lt
     * @date: 2024/4/5 15:47
     */
    public HttpResult truckDelete(String id)
    {
        KwBusinessTruck businessTruck = kwBusinessTruckMapper
                .selectOne(new LambdaQueryWrapper<KwBusinessTruck>().eq(KwBusinessTruck::getId,id)
                        .eq(KwBusinessTruck::getDelFlag, 0)
                );
        if(businessTruck!=null){
            businessTruck.setDelFlag(1);
            int i = kwBusinessTruckMapper.updateById(businessTruck);
            if(i > 0) {
                LambdaUpdateWrapper<KwBusinessTruckTransportLicense> wrapper = new LambdaUpdateWrapper<>();
                wrapper.eq(KwBusinessTruckTransportLicense::getTruckId, id);
                KwBusinessTruckTransportLicense kwBusinessTruckTransportLicense = new KwBusinessTruckTransportLicense();
                kwBusinessTruckTransportLicense.setDelFlag(1);
                kwBusinessTruckTransportLicenseMapper.update(kwBusinessTruckTransportLicense, wrapper);

                LambdaUpdateWrapper<KwBusinessTruckMore> wrapperMore = new LambdaUpdateWrapper<>();
                wrapperMore.eq(KwBusinessTruckMore::getTruckId, id).eq(KwBusinessTruckMore::getDelFlag, 0);

                KwBusinessTruckMore kwBusinessTruckMore = new KwBusinessTruckMore();
                kwBusinessTruckMore.setDelFlag(1);
                kwBusinessTruckMoreMapper.update(kwBusinessTruckMore, wrapperMore);
            }
            return HttpResult.ok("删除成功");
        }else{
            return HttpResult.ok("未找到该条数据");
        }
    }

    /**
     * @desc: 车辆详情
     * @author: lt
     * @date: 2024/4/7 9:58
     */
    public BusinessTruckDetailRes truckDetailById(String id) {
        KwBusinessTruck businessTruck = kwBusinessTruckMapper
                .selectOne(new LambdaQueryWrapper<KwBusinessTruck>().eq(KwBusinessTruck::getId, id)
                        .eq(KwBusinessTruck::getDelFlag, 0)
                );
        if (businessTruck != null) {
            BusinessTruckDetailRes businessTruckDetailRes = BeanUtil.copyProperties(businessTruck, BusinessTruckDetailRes.class);
            KwBusinessTruckTransportLicense kwBusinessTruckTransportLicense = kwBusinessTruckTransportLicenseMapper.selectOne((new LambdaQueryWrapper<KwBusinessTruckTransportLicense>()
                    .eq(KwBusinessTruckTransportLicense::getTruckId, id)
                    .eq(KwBusinessTruckTransportLicense::getDelFlag, 0)));
            if (kwBusinessTruckTransportLicense != null) {
                businessTruckDetailRes.setTransportLicense(kwBusinessTruckTransportLicense);
            }

            KwBusinessTruckMore kwBusinessTruckMore = kwBusinessTruckMoreMapper.selectOne((new LambdaQueryWrapper<KwBusinessTruckMore>().eq(KwBusinessTruckMore::getTruckId, id).eq(KwBusinessTruckMore::getDelFlag, 0)));
            if (kwBusinessTruckTransportLicense != null) {
                businessTruckDetailRes.setKwBusinessTruckMore(kwBusinessTruckMore);
            }



            return businessTruckDetailRes; // 返回正确的对象
        }
        return null; // 或者根据业务需求返回其他值
    }

    /**
     * @return
     * @desc: 获取车辆类型
     * @author: lt
     * @date: 2024/4/7 14:14
     */
    public List<KwBusinessTruckType> truckTypeList()
    {
        return kwBusinessTruckTypeMapper.selectList(new LambdaQueryWrapper<KwBusinessTruckType>().lt(KwBusinessTruckType::getDelFlag, 1));
    }

    /**
     * @return
     * @desc: 获取能源类型
     * @author: lt
     * @date: 2024/4/7 14:14
     */
    public List<KwBusinessTruckEnergy> truckEnergyList()
    {
        return kwBusinessTruckEnergyMapper.selectList(new LambdaQueryWrapper<KwBusinessTruckEnergy>().lt(KwBusinessTruckEnergy::getDelFlag, 1));
    }






}
