package com.project.zcustom.service.unit.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.project.zcustom.controller.core.PageParamVo;
import com.project.zcustom.controller.core.StringUtils;
import com.project.zcustom.domain.addional.LargeCar;
import com.project.zcustom.domain.basics.PlatAppOrg;
import com.project.zcustom.mapper.unit.LargePublicCarMapper;
import com.project.zcustom.service.service.basics.IPlatAppOrgService;
import com.project.zcustom.service.unit.ILargePublicCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class LargePublicCarServiceImpl extends ServiceImpl<LargePublicCarMapper, LargeCar> implements ILargePublicCarService {

    @Autowired
    private IPlatAppOrgService platAppOrgService;

    /**
     * 组装查询条件
     */
    private LambdaQueryWrapper<LargeCar> getLambdaQueryWrapper(LargeCar largeCar) {
        LambdaQueryWrapper<LargeCar> lqw = new LambdaQueryWrapper<LargeCar>();
        if (StringUtils.isNotBlank(largeCar.getAppEncode())){
            lqw.eq(LargeCar::getAppEncode ,largeCar.getAppEncode());
        }
        if (StringUtils.isNotNull(largeCar.getTotalCarNum())){
            lqw.eq(LargeCar::getTotalCarNum ,largeCar.getTotalCarNum());
        }
        if (StringUtils.isNotNull(largeCar.getCarNumOne())){
            lqw.eq(LargeCar::getCarNumOne ,largeCar.getCarNumOne());
        }
        if (StringUtils.isNotNull(largeCar.getCarNumTwo())){
            lqw.eq(LargeCar::getCarNumTwo ,largeCar.getCarNumTwo());
        }
        if (StringUtils.isNotNull(largeCar.getCarNumThree())){
            lqw.eq(LargeCar::getCarNumThree ,largeCar.getCarNumThree());
        }
        if (StringUtils.isNotNull(largeCar.getCarNumFour())){
            lqw.eq(LargeCar::getCarNumFour ,largeCar.getCarNumFour());
        }
        if (StringUtils.isNotNull(largeCar.getCarNumFive())){
            lqw.eq(LargeCar::getCarNumFive ,largeCar.getCarNumFive());
        }
        if (StringUtils.isNotNull(largeCar.getEnterpriseCarNum())){
            lqw.eq(LargeCar::getEnterpriseCarNum ,largeCar.getEnterpriseCarNum());
        }
        if (StringUtils.isNotNull(largeCar.getHeadCarNum())){
            lqw.eq(LargeCar::getHeadCarNum ,largeCar.getHeadCarNum());
        }
        if (StringUtils.isNotNull(largeCar.getOilCarNum())){
            lqw.eq(LargeCar::getOilCarNum ,largeCar.getOilCarNum());
        }
        if (StringUtils.isNotNull(largeCar.getNewPowerCarNum())){
            lqw.eq(LargeCar::getNewPowerCarNum ,largeCar.getNewPowerCarNum());
        }
        if (StringUtils.isNotNull(largeCar.getEstablishmentsCarNum())){
            lqw.eq(LargeCar::getEstablishmentsCarNum ,largeCar.getEstablishmentsCarNum());
        }
        if (StringUtils.isNotNull(largeCar.getYearAddNum())){
            lqw.eq(LargeCar::getYearAddNum ,largeCar.getYearAddNum());
        }
        if (StringUtils.isNotNull(largeCar.getYearReduceNum())){
            lqw.eq(LargeCar::getYearReduceNum ,largeCar.getYearReduceNum());
        }
        if (StringUtils.isNotNull(largeCar.getTerminalTotal())){
            lqw.eq(LargeCar::getTerminalTotal ,largeCar.getTerminalTotal());
        }
        if (StringUtils.isNotNull(largeCar.getTerminalInstall())){
            lqw.eq(LargeCar::getTerminalInstall ,largeCar.getTerminalInstall());
        }
        if (StringUtils.isNotNull(largeCar.getTerminalOnline())){
            lqw.eq(LargeCar::getTerminalOnline ,largeCar.getTerminalOnline());
        }
        if (StringUtils.isNotNull(largeCar.getNotArchivedNum())){
            lqw.eq(LargeCar::getNotArchivedNum ,largeCar.getNotArchivedNum());
        }
        if (StringUtils.isNotNull(largeCar.getNoPidNum())){
            lqw.eq(LargeCar::getNoPidNum ,largeCar.getNoPidNum());
        }
        if (StringUtils.isNotNull(largeCar.getOverSpeedNum())){
            lqw.eq(LargeCar::getOverSpeedNum ,largeCar.getOverSpeedNum());
        }
        if (StringUtils.isNotNull(largeCar.getHolidayNum())){
            lqw.eq(LargeCar::getHolidayNum ,largeCar.getHolidayNum());
        }
        if (StringUtils.isNotNull(largeCar.getPcdNum())){
            lqw.eq(LargeCar::getPcdNum ,largeCar.getPcdNum());
        }
        if (StringUtils.isNotNull(largeCar.getAvgPcdNum())){
            lqw.eq(LargeCar::getAvgPcdNum ,largeCar.getAvgPcdNum());
        }
        if (StringUtils.isNotNull(largeCar.getMileCount())){
            lqw.eq(LargeCar::getMileCount ,largeCar.getMileCount());
        }
        if (StringUtils.isNotNull(largeCar.getAvgMile())){
            lqw.eq(LargeCar::getAvgMile ,largeCar.getAvgMile());
        }
        return lqw;
    }

    /**
     * 分页查询公务用车列表
     *
     * @param largeCar 公务用车
     * @return 公务用车集合
     */
    @Override
    public IPage<LargeCar> pagingLargeCarList(LargeCar largeCar) {
        IPage<LargeCar> page = new Page<>(largeCar.getPageNum(), largeCar.getPageSize());
        LambdaQueryWrapper<LargeCar> lqw = getLambdaQueryWrapper(largeCar);
        // 排序 默认时间倒序
        lqw.orderByDesc(LargeCar::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 分页查询公务用车列表
     *
     * @param lqw
     * @return 公务用车集合
     */
    @Override
    public IPage<LargeCar> pagingLargeCarList(PageParamVo pageParamVo, LambdaQueryWrapper<LargeCar> lqw) {
        IPage<LargeCar> page = new Page<>(pageParamVo.getPageNum(), pageParamVo.getPageSize());
        // 排序 默认时间倒序
        lqw.orderByDesc(LargeCar::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 查询公务用车列表
     *
     * @param largeCar 公务用车
     * @return 公务用车
     */
    @Override
    public List<LargeCar> selectLargeCarList(LargeCar largeCar) {
        LambdaQueryWrapper<LargeCar> lqw = getLambdaQueryWrapper(largeCar);
        // 排序 默认时间倒序
        lqw.orderByDesc(LargeCar::getCreateTime);
        return baseMapper.selectList(lqw);
    }

    /**
     * 查询公务用车列表
     *
     * @param largeCar 公务用车
     * @return 公务用车
     */
    @Override
    public List<LargeCar> selectLargeCarList(LargeCar largeCar,Long limitNum) {
        LambdaQueryWrapper<LargeCar> lqw = getLambdaQueryWrapper(largeCar);
        // 排序 默认时间倒序
        lqw.orderByDesc(LargeCar::getCreateTime);
        lqw.last("limit "+limitNum);
        return baseMapper.selectList(lqw);
    }

    /**
     * 根据指定字段查询信息
     *
     * @param field 字段名称
     * @param filedValue 字段值
     * @return 公务用车
     */
    @Override
    public LargeCar getLargeCarByField(String field, String filedValue) {
        QueryWrapper<LargeCar> lqw = new QueryWrapper<LargeCar>();
        lqw.eq(field, filedValue);
        lqw.last("limit 1");
        return this.getOne(lqw, false);
    }

    /**
     * 根据指定字段查询信息列表
     *
     * @param field 字段名称
     * @param filedValue 字段值
     * @return 公务用车
     */
    @Override
    public List<LargeCar> selectLargeCarListByField(String field, String filedValue,Long limitNum) {
        QueryWrapper<LargeCar> lqw = new QueryWrapper<LargeCar>();
        lqw.eq(field, filedValue);
        lqw.last("limit "+limitNum);
        return this.list(lqw);
    }

    /**
     * 根据指定字段查询信息列表
     *
     * @param field 字段名称
     * @param filedValue 字段值
     * @return 公务用车
     */
    @Override
    public List<LargeCar> selectLargeCarListByField(String field, String filedValue) {
        QueryWrapper<LargeCar> lqw = new QueryWrapper<LargeCar>();
        lqw.eq(field, filedValue);
        return this.list(lqw);
    }

    /**
     * 判断指定字段值是否存在
     *
     * @param field 字段名称
     * @param filedValue 字段值
     */
    @Override
    public void checkFieldExist(String field, String filedValue,String tips, Long id){
        QueryWrapper<LargeCar> lqw = new QueryWrapper<LargeCar>();
        lqw.eq(field, filedValue);
        lqw.ne("id", id);
        if(this.getOne(lqw,false) != null){
            throw new RuntimeException(tips + "已存在");
        }
    }

    public int upload(String type, Map<String, Object> json){
        try {
            List<PlatAppOrg> orgList = platAppOrgService.selectAppCodeList();
            Map<String, String> orgMap = orgList.stream().collect(Collectors.toMap(PlatAppOrg::getAppEncode, PlatAppOrg::getAppName, (v1, v2) -> v1));
            List<LargeCar> list = JSON.parseArray(JSON.toJSONString(json.get("data")), LargeCar.class);
            for (LargeCar largeCar : list){
                if (!orgMap.containsKey(largeCar.getAppEncode())){
                    continue;
                }
                LargeCar car = this.getOne(new QueryWrapper<LargeCar>().eq("app_encode", largeCar.getAppEncode()));
                if (car != null) {
                    largeCar.setId(car.getId());
                    this.updateById(largeCar);
                } else {
                    this.save(largeCar);
                }
            }
            return 1;
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

}
