package com.sckw.fleet.dubbo;

import com.sckw.core.utils.BeanUtils;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.fleet.api.RemoteFleetService;
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();
        List<Map<String, Object>> drivers = driverDao.findList(new HashMap(){{put("ids", driverIds);}});
        if (CollectionUtils.isEmpty(drivers)) {
            return driverMap;
        }

        /**数据处理**/
        for (Map<String, Object> driver:drivers) {
            RDriverVo driverVo = new RDriverVo();
            driverVo.setId(driver.get("id") != null ? Long.parseLong(String.valueOf(driver.get("id"))) : null);
            driverVo.setName(driver.get("name") != null ? String.valueOf(driver.get("name")) : null);
            driverVo.setPhone(driver.get("phone") != null ? String.valueOf(driver.get("phone")) : null);
            driverVo.setIdcard(driver.get("idcard") != null ? String.valueOf(driver.get("idcard")) : null);
            driverVo.setTotalComplete(driver.get("totalComplete") != null ? Integer.parseInt(String.valueOf(driver.get("totalComplete"))) : null);
            driverVo.setTotalTake(driver.get("totalTake") != null ? Integer.parseInt(String.valueOf(driver.get("totalTake"))) : null);
            driverVo.setTotalWeight(driver.get("totalWeight") != null ? new BigDecimal(Double.parseDouble(String.valueOf(driver.get("totalWeight")))) : null);
            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 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();
        List<Map<String, Object>> trucks = truckDao.findList(new HashMap(){{put("truckNos", truckNos);}});
        if (CollectionUtils.isEmpty(trucks)) {
            return truckMap;
        }

        /**数据处理**/
        for (Map<String, Object> truck:trucks) {
            RTruckVo truckVo = new RTruckVo();
            truckVo.setId(truck.get("id") != null ? Long.parseLong(String.valueOf(truck.get("id"))) : null);
            truckVo.setTruckNo(truck.get("truckNo") != null ? String.valueOf(truck.get("truckNo")) : null);
            truckVo.setActualWeight(truck.get("actualWeight") != null ? Double.parseDouble(String.valueOf(truck.get("actualWeight"))) : null);
            truckVo.setTrailerNo(truck.get("trailerNo") != null ? String.valueOf(truck.get("trailerNo")) : null);
            truckVo.setTotalComplete(truck.get("totalComplete") != null ? Integer.parseInt(String.valueOf(truck.get("totalComplete"))) : null);
            truckVo.setTotalTake(truck.get("totalTake") != null ? Integer.parseInt(String.valueOf(truck.get("totalTake"))) : null);
            truckVo.setTotalWeight(truck.get("totalWeight") != null ? new BigDecimal(Double.parseDouble(String.valueOf(truck.get("totalWeight")))) : null);
            truckVo.setBusinessStatus(truck.get("businessStatus") != null ? Integer.parseInt(String.valueOf(truck.get("businessStatus"))) : null);
            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);
    }
}
