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.common.core.domain.search.PageParamVo;
import com.project.common.utils.StringUtils;
import com.project.zcustom.domain.addional.LargeCarRank;
import com.project.zcustom.domain.addional.LargeWarnDetail;
import com.project.zcustom.domain.basics.PlatAppOrg;
import com.project.zcustom.mapper.car.PlatCarMapper;
import com.project.zcustom.mapper.unit.LargeWarnDetailMapper;
import com.project.zcustom.service.service.basics.IPlatAppOrgService;
import com.project.zcustom.service.unit.ILargeWarnDetailService;
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 LargeWarnDetailServiceImpl extends ServiceImpl<LargeWarnDetailMapper, LargeWarnDetail> implements ILargeWarnDetailService {

    @Autowired
    private IPlatAppOrgService platAppOrgService;

    @Autowired
    private PlatCarMapper platCarMapper;

    /**
     * 组装查询条件
     */
    private LambdaQueryWrapper<LargeWarnDetail> getLambdaQueryWrapper(LargeWarnDetail largeWarnDetail) {
        LambdaQueryWrapper<LargeWarnDetail> lqw = new LambdaQueryWrapper<LargeWarnDetail>();
        if (StringUtils.isNotBlank(largeWarnDetail.getAppEncode())){
            lqw.eq(LargeWarnDetail::getAppEncode ,largeWarnDetail.getAppEncode());
        }
        if (StringUtils.isNotNull(largeWarnDetail.getVehicleNum())){
            lqw.eq(LargeWarnDetail::getVehicleNum ,largeWarnDetail.getVehicleNum());
        }
        if (StringUtils.isNotNull(largeWarnDetail.getAlertType())){
            lqw.eq(LargeWarnDetail::getAlertType ,largeWarnDetail.getAlertType());
        }
        if (StringUtils.isNotNull(largeWarnDetail.getStartTime())){
            lqw.eq(LargeWarnDetail::getStartTime ,largeWarnDetail.getStartTime());
        }
        if (StringUtils.isNotNull(largeWarnDetail.getEndTime())){
            lqw.eq(LargeWarnDetail::getEndTime ,largeWarnDetail.getEndTime());
        }
        return lqw;
    }

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

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

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

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

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

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

    /**
     * 根据指定字段查询信息列表
     *
     * @param field 字段名称
     * @param filedValue 字段值
     * @return 公务用车预警明细
     */
    @Override
    public List<LargeWarnDetail> selectLargeWarnDetailListByField(String field, String filedValue) {
        QueryWrapper<LargeWarnDetail> lqw = new QueryWrapper<LargeWarnDetail>();
        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<LargeWarnDetail> lqw = new QueryWrapper<LargeWarnDetail>();
        lqw.eq(field, filedValue);
        lqw.ne("id", id);
        if(this.getOne(lqw,false) != null){
            throw new RuntimeException(tips + "已存在");
        }
    }

    public int upload(Map<String, Object> json, String alertType){
        try {
            List<PlatAppOrg> orgList = platAppOrgService.selectAppCodeList();
            Map<String, String> orgMap = orgList.stream().collect(Collectors.toMap(PlatAppOrg::getAppEncode, PlatAppOrg::getAppParentOrg, (v1, v2) -> v1));
            List<LargeWarnDetail> list = JSON.parseArray(JSON.toJSONString(json.get("data")), LargeWarnDetail.class);
            platCarMapper.deleteAllWarnDetail(alertType);
            for (LargeWarnDetail largeWarnDetail : list){
                if (!orgMap.containsKey(largeWarnDetail.getAppEncode()) || !orgMap.get(largeWarnDetail.getAppEncode()).equals("10001")){
                    continue;
                }
                this.save(largeWarnDetail);
            }
            return 1;
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }
}
