package com.xxfc.platform.vehicle.biz;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.wxiaoqi.security.common.biz.BaseBiz;
import com.github.wxiaoqi.security.common.exception.BaseException;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import com.xxfc.platform.vehicle.constant.ResCode.ResCode;
import com.xxfc.platform.vehicle.entity.Vehicle;
import com.xxfc.platform.vehicle.entity.VehicleDepartureLog;
import com.xxfc.platform.vehicle.mapper.VehicleDepartureLogMapper;
import com.xxfc.platform.vehicle.mapper.VehicleMapper;
import com.xxfc.platform.vehicle.pojo.DepartureLogVo;
import com.xxfc.platform.vehicle.pojo.VehicleDepartureLogVo;
import com.xxfc.platform.vehicle.pojo.VehicleDepartureStatisticDataVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.weekend.WeekendSqls;

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

@Service
@Slf4j
public class VehicleDepartureService extends BaseBiz<VehicleDepartureLogMapper, VehicleDepartureLog> {

    @Autowired
    VehicleMapper vehicleMapper;

    public PageInfo<VehicleDepartureLogVo> page(String numberPlate,Integer code,Integer zoneId,Integer departureId,Integer arrivalBranchCompanyId,String use ,String startTime,String endTime, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        return new PageInfo<>(mapper.selectVoAll(numberPlate,code,zoneId,departureId,arrivalBranchCompanyId,use,startTime,endTime));
    }

    public PageInfo<VehicleDepartureLogVo> pageNotAllData(String numberPlate,Integer code,Integer zoneId,Integer departureId,Integer arrivalBranchCompanyId,String use , String startTime,String endTime,  Integer page, Integer limit, List<Integer> companyList) {
        PageHelper.startPage(page, limit);
        if (companyList == null || companyList.size() == 0) {
            companyList = Arrays.asList(-1);
        }
        return new PageInfo<>(mapper.selectVoAllNotAllData(numberPlate,code,zoneId,departureId,arrivalBranchCompanyId,use,startTime, endTime, companyList));
    }

    public PageInfo<VehicleDepartureLogVo> findByVehicle(String vehicleId, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        return new PageInfo<>(mapper.selectByVehicleId(vehicleId));
    }

    public VehicleDepartureLogVo getByRecordId(Long vehicleBookRecordId) {
        return mapper.selectByBookRecordId(vehicleBookRecordId);
    }

    public VehicleDepartureStatisticDataVo statistic(String numberPlate) {
        Vehicle vehicle = vehicleMapper.selectByNumberPlate(numberPlate);
        if (vehicle == null) {
            throw new BaseException(ResCode.VEHICLE_DEPARTURE_VEHICLE_UNEXIST.getDesc(),
                    ResCode.VEHICLE_DEPARTURE_VEHICLE_UNEXIST.getCode());
        }

        VehicleDepartureStatisticDataVo statisticData = new VehicleDepartureStatisticDataVo();
        // 出行次数
        VehicleDepartureLog departureLog = new VehicleDepartureLog();
        departureLog.setVehicleId(vehicle.getId());
        statisticData.setDepartureCount(mapper.selectCount(departureLog));
        // 出行天数
        statisticData.setDepartureDay(mapper.selectDayByVehicleId(vehicle.getId()));
        // 出行公里数
        statisticData.setDepartureMileage(mapper.selectMileageByVehicleId(vehicle.getId()));
        return statisticData;
    }


    @Transactional
    public ObjectRestResponse save(VehicleDepartureLog vehicleDepartureLog) {
        Integer id = vehicleDepartureLog.getId();
        log.info("添加出行记录参数： vehicleDepartureLog = {}", vehicleDepartureLog);
        if (id == null || id == 0) {
            vehicleDepartureLog.setCreateTime(new Date());
            vehicleDepartureLog.setState(0);
            insertSelective(vehicleDepartureLog);
            return ObjectRestResponse.succ();
        } else {
            vehicleDepartureLog.setUpdateTime(new Date());
            VehicleDepartureLog oldValue = mapper.selectByPrimaryKey(vehicleDepartureLog);
            if (oldValue != null) {
                log.info("更新出行记录： vehicleDepartureLog = {}", oldValue);
                BeanUtil.copyProperties(vehicleDepartureLog, oldValue, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
                oldValue.setUpdateTime(new Date());
                updateSelectiveById(oldValue);
                return ObjectRestResponse.succ();
            } else {
                vehicleDepartureLog.setCreateTime(new Date());
                vehicleDepartureLog.setState(0);
                insertSelective(vehicleDepartureLog);
                return ObjectRestResponse.succ();
            }

        }

    }

    public Integer selectAllDepartureLog(Map<String, Object> param) {
        return mapper.selectAllDepartureLog(param);
    }

    public ObjectRestResponse findOne(Integer vid) throws Exception {
        Example exm = Example.builder(VehicleDepartureLog.class)
                .where(WeekendSqls.<VehicleDepartureLog>custom()
                        .andEqualTo(VehicleDepartureLog::getVehicleId, vid)
                        .andEqualTo(VehicleDepartureLog::getState, 0))
                .orderByDesc("create_time")
                .build();

        List<VehicleDepartureLog> vehicleDepartureLogs = mapper.selectByExample(exm);
        if (vehicleDepartureLogs.size() == 1) {
            VehicleDepartureLog vehicleDepartureLog = vehicleDepartureLogs.get(0);
            if (vehicleDepartureLog != null) {
                return ObjectRestResponse.succ(vehicleDepartureLog);
            }
        }
        return ObjectRestResponse.createFailedResult(ResCode.FIND_DATA_NOT_EXIST.getCode(), ResCode.FIND_DATA_NOT_EXIST.getDesc());
    }

    @Transactional
    public ObjectRestResponse collect(VehicleDepartureLog vdl) {
        vdl.setArrivalTime(new Date());
        vdl.setState(1);
        updateSelectiveById(vdl);
        return ObjectRestResponse.succ();
    }


}
