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

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.jx.utils.DateUtils;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import cn.iocoder.yudao.module.jx.domain.DriveSchoolFacility;
import cn.iocoder.yudao.module.jx.mapper.DriveSchoolFacilityMapper;
import cn.iocoder.yudao.module.jx.service.IDriveSchoolFacilityService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 设备管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-23
 */
@Service
public class DriveSchoolFacilityServiceImpl implements IDriveSchoolFacilityService
{
    @Autowired
    private DriveSchoolFacilityMapper driveSchoolFacilityMapper;

    @Resource
    private AdminUserApi userApi;

    /**
     * 查询设备管理
     *
     * @param id 设备管理主键
     * @return 设备管理
     */
    @Override
    public DriveSchoolFacility selectDriveSchoolFacilityById(Long id)
    {
        return driveSchoolFacilityMapper.selectDriveSchoolFacilityById(id);
    }

    /**
     * 查询设备管理列表
     *
     * @param driveSchoolFacility 设备管理
     * @return 设备管理
     */
    @Override
    public IPage<DriveSchoolFacility> selectDriveSchoolFacilityList(DriveSchoolFacility driveSchoolFacility, Page<DriveSchoolFacility> page)
    {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserRespDTO user = userApi.getUser(userId);
        Long deptId = user.getDeptId();
        driveSchoolFacility.setDeptId(deptId);
        if (deptId ==100){
            driveSchoolFacility.setDeptId(null);
        }
        return driveSchoolFacilityMapper.selectDriveSchoolFacilityList(driveSchoolFacility,page);
    }


//    @Scheduled(cron = "0 0 7 * * ?")
//    public void onTimme()
//    {
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        List<DriveSchoolFacility> driveSchoolFacilities = driveSchoolFacilityMapper.selectDriveSchoolFacilityList(new DriveSchoolFacility());
//        for (DriveSchoolFacility driveSchoolFacility : driveSchoolFacilities) {
//            if(ObjectUtils.isNotEmpty(driveSchoolFacility.getYearcheckTime() )&& ObjectUtils.isNotEmpty(driveSchoolFacility.getPhone()) && ObjectUtils.isNotEmpty(driveSchoolFacility.getCustodian())){
//                String format = simpleDateFormat.format(new Date());
//                //如果当天是年检时间就发短信通知
////                if (format.equals(simpleDateFormat.format(driveSchoolFacility.getYearcheckTime()))){
////                    SendSmsUtil.sendMsgCommon(new String[]{driveSchoolFacility.getCustodian()+"  "+driveSchoolFacility.getCustodian(),format+" "+(format)}, driveSchoolFacility.getPhone(),"1400852709","蓝安汽车小程序","2143603");
////                }
//            }
//        }
//    }

    /**
     * 新增设备管理
     *
     * @param driveSchoolFacility 设备管理
     * @return 结果
     */
    @Override
    public int insertDriveSchoolFacility(DriveSchoolFacility driveSchoolFacility)
    {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserRespDTO user = userApi.getUser(userId);
        Long deptId = user.getDeptId();
        driveSchoolFacility.setDeptId(deptId);
        //driveSchoolFacility.setCreateTime(DateUtils.getNowDate());
        return driveSchoolFacilityMapper.insertDriveSchoolFacility(driveSchoolFacility);
    }

    /**
     * 修改设备管理
     *
     * @param driveSchoolFacility 设备管理
     * @return 结果
     */
    @Override
    public int updateDriveSchoolFacility(DriveSchoolFacility driveSchoolFacility)
    {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserRespDTO user = userApi.getUser(userId);
        Long deptId = user.getDeptId();
        driveSchoolFacility.setDeptId(deptId);
        //driveSchoolFacility.setUpdateTime(DateUtils.getNowDate());
        return driveSchoolFacilityMapper.updateDriveSchoolFacility(driveSchoolFacility);
    }

    /**
     * 批量删除设备管理
     *
     * @param ids 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteDriveSchoolFacilityByIds(Long[] ids)
    {
        return driveSchoolFacilityMapper.deleteDriveSchoolFacilityByIds(ids);
    }

    /**
     * 删除设备管理信息
     *
     * @param id 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteDriveSchoolFacilityById(Long id)
    {
        return driveSchoolFacilityMapper.deleteDriveSchoolFacilityById(id);
    }
}
