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.LargeCar;
import com.project.zcustom.domain.addional.LargeCarDetail;
import com.project.zcustom.domain.addional.LargeCarRank;
import com.project.zcustom.domain.basics.PlatAppOrg;
import com.project.zcustom.mapper.car.PlatCarMapper;
import com.project.zcustom.mapper.unit.LargeCarDetailMapper;
import com.project.zcustom.mapper.unit.LargePublicCarMapper;
import com.project.zcustom.service.service.basics.IPlatAppOrgService;
import com.project.zcustom.service.unit.ILargeCarDetailService;
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 LargeCarDetailServiceImpl extends ServiceImpl<LargeCarDetailMapper, LargeCarDetail> implements ILargeCarDetailService {
    @Autowired
    private IPlatAppOrgService platAppOrgService;

    @Autowired
    private PlatCarMapper platCarMapper;

    /**
     * 组装查询条件
     */
    private LambdaQueryWrapper<LargeCarDetail> getLambdaQueryWrapper(LargeCarDetail largeCarDetail) {
        LambdaQueryWrapper<LargeCarDetail> lqw = new LambdaQueryWrapper<LargeCarDetail>();
        if (StringUtils.isNotBlank(largeCarDetail.getAppEncode())){
            lqw.eq(LargeCarDetail::getAppEncode ,largeCarDetail.getAppEncode());
        }
        if (StringUtils.isNotNull(largeCarDetail.getVehicleNum())){
            lqw.eq(LargeCarDetail::getVehicleNum ,largeCarDetail.getVehicleNum());
        }
        if (StringUtils.isNotBlank(largeCarDetail.getUseProperty())){
            lqw.eq(LargeCarDetail::getUseProperty ,largeCarDetail.getUseProperty());
        }
        if (StringUtils.isNotBlank(largeCarDetail.getAssetsCard())){
            lqw.eq(LargeCarDetail::getAssetsCard ,largeCarDetail.getAssetsCard());
        }
        if (StringUtils.isNotNull(largeCarDetail.getOrgDate())){
            lqw.eq(LargeCarDetail::getOrgDate ,largeCarDetail.getOrgDate());
        }
        if (StringUtils.isNotNull(largeCarDetail.getHandleDate())){
            lqw.eq(LargeCarDetail::getHandleDate ,largeCarDetail.getHandleDate());
        }
        if (StringUtils.isNotBlank(largeCarDetail.getHandleReason())){
            lqw.eq(LargeCarDetail::getHandleReason ,largeCarDetail.getHandleReason());
        }
        if (StringUtils.isNotNull(largeCarDetail.getFlag())){
            lqw.eq(LargeCarDetail::getFlag ,largeCarDetail.getFlag());
        }
        return lqw;
    }

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

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

    /**
     * 查询公务用车年度增加减少列表
     *
     * @param largeCarDetail 公务用车年度增加减少
     * @return 公务用车年度增加减少
     */
    @Override
    public List<LargeCarDetail> selectLargeCarDetailList(LargeCarDetail largeCarDetail) {
        LambdaQueryWrapper<LargeCarDetail> lqw = getLambdaQueryWrapper(largeCarDetail);
        // 排序 默认时间倒序
        lqw.orderByDesc(LargeCarDetail::getCreateTime);
        return baseMapper.selectList(lqw);
    }

    /**
     * 查询公务用车年度增加减少列表
     *
     * @param largeCarDetail 公务用车年度增加减少
     * @return 公务用车年度增加减少
     */
    @Override
    public List<LargeCarDetail> selectLargeCarDetailList(LargeCarDetail largeCarDetail,Long limitNum) {
        LambdaQueryWrapper<LargeCarDetail> lqw = getLambdaQueryWrapper(largeCarDetail);
        // 排序 默认时间倒序
        lqw.orderByDesc(LargeCarDetail::getCreateTime);
        lqw.last("limit "+limitNum);
        return baseMapper.selectList(lqw);
    }

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

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

    /**
     * 根据指定字段查询信息列表
     *
     * @param field 字段名称
     * @param filedValue 字段值
     * @return 公务用车年度增加减少
     */
    @Override
    public List<LargeCarDetail> selectLargeCarDetailListByField(String field, String filedValue) {
        QueryWrapper<LargeCarDetail> lqw = new QueryWrapper<LargeCarDetail>();
        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<LargeCarDetail> lqw = new QueryWrapper<LargeCarDetail>();
        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, Integer flag){
        try {
            List<PlatAppOrg> orgList = platAppOrgService.selectAppCodeList();
            Map<String, String> orgMap = orgList.stream().collect(Collectors.toMap(PlatAppOrg::getAppEncode, PlatAppOrg::getAppParentOrg, (v1, v2) -> v1));
            List<LargeCarDetail> list = JSON.parseArray(JSON.toJSONString(json.get("data")), LargeCarDetail.class);
            platCarMapper.deleteAllCarDetail(flag);
            for (LargeCarDetail largeCarDetail : list){
                if (!orgMap.containsKey(largeCarDetail.getAppEncode()) || !orgMap.get(largeCarDetail.getAppEncode()).equals("10001")){
                    continue;
                }
                largeCarDetail.setFlag(flag);
                this.save(largeCarDetail);
            }
            return 1;
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }
}
