package com.sckw.fleet.dubbo;

import com.sckw.core.model.constant.Global;
import com.sckw.core.utils.BeanUtils;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.core.utils.NumberUtils;
import com.sckw.core.utils.StringUtils;
import com.sckw.fleet.api.RemoteFleetService;
import com.sckw.fleet.api.model.vo.RDriverDetailVo;
import com.sckw.fleet.api.model.vo.RDriverVo;
import com.sckw.fleet.api.model.vo.RTruckVo;
import com.sckw.fleet.dao.KwfDriverMapper;
import com.sckw.fleet.dao.KwfTruckMapper;
import com.sckw.fleet.model.KwfDriver;
import com.sckw.fleet.model.KwfTruck;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zk
 * @desc 车队服务
 * @date 2023/7/18 0018
 */
@DubboService(group = "design", version = "1.0.0")
@Service
public class RemoteFleetServiceImpl implements RemoteFleetService {

    @Autowired
    KwfTruckMapper truckDao;
    @Autowired
    KwfDriverMapper driverDao;

    /**
     * @param driverIds 司机档案主键id，多个已逗号隔开
     * @desc 查询司机信息
     * @author zk
     * @date 2023/7/18
     **/
    @Override
    public Map<Long, RDriverVo> findDriver(List<Long> driverIds) {
        /**获取数据**/
        Map<Long, RDriverVo> driverMap = new HashMap<>(Global.NUMERICAL_SIXTEEN);
        List<KwfDriver> drivers = driverDao.findDriver(new HashMap<>(Global.NUMERICAL_SIXTEEN) {{
            put("ids", driverIds);
        }});
        if (CollectionUtils.isEmpty(drivers)) {
            return driverMap;
        }

        /**数据处理**/
        for (KwfDriver driver : drivers) {
            RDriverVo driverVo = new RDriverVo();
            driverVo.setId(driver.getId());
            driverVo.setName(driver.getName());
            driverVo.setPhone(driver.getPhone());
            driverVo.setIdcard(driver.getIdcard());
            driverVo.setTotalComplete(driver.getTotalComplete());
            driverVo.setTotalTake(driver.getTotalTake());
            driverVo.setTotalWeight(driver.getTotalWeight());
            driverVo.setBusinessStatus(driver.getBusinessStatus());
            driverMap.put(driverVo.getId(), driverVo);
        }
        return driverMap;
    }

    /**
     * @param driverId 司机档案主键id
     * @desc 查询司机信息
     * @author zk
     * @date 2023/8/3
     **/
    @Override
    public RDriverVo findDriver(Long driverId) {
        RDriverVo driverVo = new RDriverVo();
        KwfDriver driver = driverDao.selectById(driverId);
        BeanUtils.copyProperties(driver, driverVo);
        return driverVo;
    }

    /**
     * @param phone 司机档手机号
     * @desc 查询司机信息
     * @author zk
     * @date 2023/8/10
     **/
    @Override
    public RDriverDetailVo findDriverDetai(String phone) {
        List<KwfDriver> drivers = driverDao.findDriver(new HashMap<>(Global.NUMERICAL_SIXTEEN) {{
            put("phone", phone);
        }});
        if (CollectionUtils.isEmpty(drivers)) {
            return null;
        }
        KwfDriver driver = drivers.get(0);
        RDriverDetailVo driverDetail = new RDriverDetailVo();
        //BeanUtils.copyPropertiesValue(driver, driverDetail);
        BeanUtils.copyProperties(driver, driverDetail);
        return driverDetail;
    }

    /**
     * @param params 司机档案信息
     * @desc 更新司机信息
     * @author zk
     * @date 2023/8/3
     **/
    @Override
    public int updateById(RDriverVo params) {
        KwfDriver driver = new KwfDriver();
        BeanUtils.copyProperties(params, driver);
        return driverDao.updateById(driver);
    }

    /**
     * @param truckNos 车牌号，多个已逗号隔开
     * @desc 查询车辆信息
     * @author zk
     * @date 2023/7/18
     **/
    @Override
    public Map<String, RTruckVo> findTruck(List<String> truckNos) {
        /**获取数据**/
        Map<String, RTruckVo> truckMap = new HashMap<>(Global.NUMERICAL_SIXTEEN);
        List<KwfTruck> trucks = truckDao.findTruck(new HashMap<>(Global.NUMERICAL_SIXTEEN){{put("truckNos", truckNos);}});
        if (CollectionUtils.isEmpty(trucks)) {
            return truckMap;
        }

        /**数据处理**/
        for (KwfTruck truck : trucks) {
            RTruckVo truckVo = new RTruckVo();
            truckVo.setId(truck.getId());
            truckVo.setTruckNo(truck.getTruckNo());
            truckVo.setActualWeight(truck.getActualWeight() != null ? truck.getActualWeight().doubleValue() : null );
            truckVo.setTrailerNo(truck.getTrailerNo());
            truckVo.setTotalComplete(truck.getTotalComplete());
            truckVo.setTotalTake(truck.getTotalTake());
            truckVo.setTotalWeight(truck.getTotalWeight());
            truckVo.setBusinessStatus(truck.getBusinessStatus());
            truckVo.setTruckType(StringUtils.objectStr(truck.getType()));
            truckMap.put(truckVo.getTruckNo(), truckVo);
        }
        return truckMap;
    }

    /**
     * @param truckId 车辆档案主键id
     * @desc 查询车辆信息
     * @author zk
     * @date 2023/8/3
     **/
    @Override
    public RTruckVo findTruck(Long truckId) {
        RTruckVo truckVo = new RTruckVo();
        KwfTruck truck = truckDao.selectById(truckId);
        BeanUtils.copyProperties(truck, truckVo);
        return truckVo;
    }

    /**
     * @param params 车辆档案信息
     * @desc 更新车辆档案
     * @author zk
     * @date 2023/8/3
     **/
    @Override
    public int updateById(RTruckVo params) {
        KwfTruck truck = new KwfTruck();
        BeanUtils.copyProperties(params, truck);
        return truckDao.updateById(truck);
    }

    /**
     * @param entIds            企业id集
     * @param capacityAmountMin 最小合计载重
     * @param capacityAmountMax 最大合计载重
     * @desc 企业运力统计
     * @author zk
     * @date 2023/9/4
     **/
    @Override
    public List<Map<String, Object>> capacityStatistics(List<Long> entIds, Integer capacityAmountMin, Integer capacityAmountMax) {
        return truckDao.capacityStatistics(entIds, capacityAmountMin, capacityAmountMax);
    }
}
