package cn.iocoder.yudao.module.smartfactory.service.staffcertificateinformation;

import cn.iocoder.yudao.framework.common.Constants;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
import cn.iocoder.yudao.module.smartfactory.controller.admin.staffcertificateinformation.vo.StaffCertificateInformationPageReqVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.staffcertificateinformation.vo.StaffCertificateInformationSaveReqVO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.staffcertificateinformation.StaffCertificateInformationDO;
import cn.iocoder.yudao.module.smartfactory.dal.mysql.staffcertificateinformation.StaffCertificateInformationMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

/**
 * 叉车员工证件信息 Service 实现类
 *
 * @author 艾楷
 */
@Service
@Validated
public class StaffCertificateInformationServiceImpl extends ServiceImpl<StaffCertificateInformationMapper, StaffCertificateInformationDO> implements StaffCertificateInformationService {

    @Resource
    private StaffCertificateInformationMapper staffCertificateInformationMapper;

    @Override
    public Long createStaffCertificateInformation(StaffCertificateInformationSaveReqVO createReqVO) {
        // 插入
        StaffCertificateInformationDO staffCertificateInformation = BeanUtils.toBean(createReqVO, StaffCertificateInformationDO.class);
        staffCertificateInformationMapper.insert(staffCertificateInformation);
        // 返回
        return staffCertificateInformation.getId();
    }

    @Override
    public void updateStaffCertificateInformation(StaffCertificateInformationSaveReqVO updateReqVO) {
        // 更新
        StaffCertificateInformationDO updateObj = BeanUtils.toBean(updateReqVO, StaffCertificateInformationDO.class);
        // 判断时间是否有过改动 - 有的话 设置为未提醒
        StaffCertificateInformationDO staffCertificateInformation = this.getStaffCertificateInformation(updateObj.getId());
        String oldTimeStr = staffCertificateInformation.getForkliftAnnualInspectionExpiredTime().format(Constants.REPO_DATE_FORMAT);
        String newTimeStr = updateObj.getForkliftAnnualInspectionExpiredTime().format(Constants.REPO_DATE_FORMAT);
        if (!oldTimeStr.equals(newTimeStr)) {
            updateObj.setReminderFlag(0);
        }
        staffCertificateInformationMapper.updateById(updateObj);
    }

    @Override
    public void deleteStaffCertificateInformation(Long id) {
        // 删除
        staffCertificateInformationMapper.deleteById(id);
    }

    @Override
    public StaffCertificateInformationDO getStaffCertificateInformation(Long id) {
        return staffCertificateInformationMapper.getStaffCertificateInformation(id);
    }

    @Override
    public PageResult<StaffCertificateInformationDO> getStaffCertificateInformationPage(StaffCertificateInformationPageReqVO pageReqVO) {
        IPage<StaffCertificateInformationDO> page = staffCertificateInformationMapper.getStaffCertificateInformationPage(MyBatisUtils.buildPage(pageReqVO), pageReqVO);
        return new PageResult<>(page.getRecords(), page.getTotal());
    }

    @Override
    public StaffCertificateInformationDO getByStaffId(Long staffId) {
        return staffCertificateInformationMapper.selectOne(new LambdaQueryWrapper<StaffCertificateInformationDO>()
                .eq(StaffCertificateInformationDO::getStaffId, staffId));
    }

}
