package cn.iocoder.yudao.module.rescue.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.module.rescue.domain.RescueCarInfo;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.module.rescue.mapper.RescueCarInfoMapper;
import cn.iocoder.yudao.module.system.api.dict.DictDataApi;
import cn.iocoder.yudao.module.rescue.service.IRescueCarInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;

/**
 * 救援车辆信息Service业务层处理
 *
 * @author zcy
 * @date 2023-09-12
 */
@Service
public class RescueCarInfoServiceImpl extends ServiceImpl<RescueCarInfoMapper, RescueCarInfo> implements IRescueCarInfoService {
    @Autowired
    private DictDataApi dictDataService;

    /**
     * 查询救援车辆信息
     *
     * @param id 救援车辆信息主键
     * @return 救援车辆信息
     */
    @Override
    public RescueCarInfo selectRescueCarInfoById(Long id) {
        RescueCarInfo carInfo = baseMapper.selectRescueCarInfoById(id);
        try {
            String temp = dictDataService.getDictDataLabel("jyc_type", carInfo.getRescueCarType());
            carInfo.setRescueCarTypeStr(temp);
            String temp2 = dictDataService.getDictDataLabel("car_license_color", carInfo.getCarLicenseColor());
            carInfo.setCarLicenseColorStr(temp2);
        } catch (Exception e) {

        }
        return carInfo;
    }

    @Override
    public RescueCarInfo selectRescueCarInfoByNum(String carNum) {
        LambdaQueryWrapper<RescueCarInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RescueCarInfo::getRescueCarNum, carNum).last("limit 1");
        return this.getOne(queryWrapper);

    }

    /**
     * 查询救援车辆信息列表
     *
     * @param rescueCarInfo 救援车辆信息
     * @return 救援车辆信息
     */
    @Override
    public IPage<RescueCarInfo> selectRescueCarInfoList(RescueCarInfo rescueCarInfo, Page<RescueCarInfo> page) {
        IPage<RescueCarInfo> rescueCarInfos = baseMapper.selectRescueCarInfoList(rescueCarInfo, page);
        for (RescueCarInfo carInfo : rescueCarInfos.getRecords()) {
            try {
                String temp = dictDataService.getDictDataLabel("jyc_type", carInfo.getRescueCarType());
                carInfo.setRescueCarTypeStr(temp);
            } catch (Exception e) {

            }
        }
        return rescueCarInfos;
    }

    /**
     * 新增救援车辆信息
     *
     * @param rescueCarInfo 救援车辆信息
     * @return 结果
     */
    @Override
    public int insertRescueCarInfo(RescueCarInfo rescueCarInfo) {
        //获取当前登录用户
        LoginUser loginUser = getLoginUser();
        return baseMapper.insert(rescueCarInfo);
    }

    /**
     * 修改救援车辆信息
     *
     * @param rescueCarInfo 救援车辆信息
     * @return 结果
     */
    @Override
    public int updateRescueCarInfo(RescueCarInfo rescueCarInfo) {
        return baseMapper.updateRescueCarInfo(rescueCarInfo);
    }

    /**
     * 批量删除救援车辆信息
     *
     * @param ids 需要删除的救援车辆信息主键
     * @return 结果
     */
    @Override
    public int deleteRescueCarInfoByIds(Long[] ids) {
        List<Long> longs = Arrays.asList(ids);
        return baseMapper.deleteByIds(longs);
    }

    /**
     * 删除救援车辆信息信息
     *
     * @param id 救援车辆信息主键
     * @return 结果
     */
    @Override
    public int deleteRescueCarInfoById(Long id) {
        return baseMapper.deleteRescueCarInfoById(id);
    }

    @Override
    public List<String> warnList(RescueCarInfo rescueCarInfo) {
        //获取当前登录用户
        // 获取当前日期
        LocalDate today = LocalDate.now();
        System.out.println("Today's date: " + today);

        // 计算30天后的日期
        LocalDate thirtyDaysLater = today.plusDays(30);
        String format = DateUtil.format(thirtyDaysLater.atStartOfDay(), "yyyy-MM-dd");
        //先获取保养即将到期的
        List<RescueCarInfo> warnListBy = baseMapper.warnListBy(format);
        //先获取保险即将到期的
        List<RescueCarInfo> warnListBx = baseMapper.warnListBx(format);
        //先获取年检即将到期的
        List<RescueCarInfo> warnListNj = baseMapper.warnListNj(format);
        List<String> res = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(warnListBy)) {
            for (RescueCarInfo carInfo : warnListBy) {
                res.add(carInfo.getRescueCarNum() + "的保养将在" + DateUtil.format(carInfo.getCarKeepTime(), "yyyy-MM-dd") + "到期，请及时保养！");
            }
        }
        if (CollectionUtil.isNotEmpty(warnListBx)) {
            for (RescueCarInfo carInfo : warnListBx) {
                res.add(carInfo.getRescueCarNum() + "的保险将在" + DateUtil.format(carInfo.getCarInsuranceTime(), "yyyy-MM-dd") + "到期，请及时续保！");
            }
        }
        if (CollectionUtil.isNotEmpty(warnListNj)) {
            for (RescueCarInfo carInfo : warnListNj) {
                res.add(carInfo.getRescueCarNum() + "的年检将在" + DateUtil.format(carInfo.getCarCheckTime(), "yyyy-MM-dd") + "到期，请及时审车！");
            }
        }
        return res;
    }

    /**
     * 救援大屏获取车辆预警接口
     *
     * @param type keep 保养  insurance 保险 check 年审
     * @return
     */
    @Override
    public List<Map<String, Object>> getRescueCarEarlyWarning(String type) {
        if (ObjectUtil.isEmpty(type)){
            return null;
        }
        return baseMapper.selectRescueCarEarlyWarning(type);
    }
}
