package com.usky.fire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usky.common.core.bean.CommonPage;

import com.usky.common.security.utils.SecurityUtils;
import com.usky.fire.domain.PatrolInspectionType;
import com.usky.fire.mapper.PatrolInspectionTypeMapper;
import com.usky.fire.service.PatrolInspectionTypeService;
import com.usky.common.mybatis.core.AbstractCrudService;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * 类型表 服务实现类
 * </p>
 *
 * @author fu
 * @since 2024-02-05
 */
@Service
public class PatrolInspectionTypeServiceImpl extends AbstractCrudService<PatrolInspectionTypeMapper, PatrolInspectionType> implements PatrolInspectionTypeService {

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public CommonPage<PatrolInspectionType> pageList(Integer pageNum, Integer pageSize) {
        IPage<PatrolInspectionType> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<PatrolInspectionType> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PatrolInspectionType::getTenantId,SecurityUtils.getTenantId())
                .eq(PatrolInspectionType::getDeleteFlag,0)
                .orderByDesc(PatrolInspectionType::getId);
        page = this.page(page,queryWrapper);
        return new CommonPage<>(page.getRecords(),page.getTotal(),pageNum,pageSize);
    }

    /**
     * 添加操作类型
     * @param patrolInspectionType
     */
    @Override
    public void add(PatrolInspectionType patrolInspectionType) {
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        Date date = new Date();
        String timestamp = dateFormat.format(date);
        patrolInspectionType.setOperateCode(patrolInspectionType.getOperateCode()+"-"+timestamp);
        baseMapper.insert(patrolInspectionType);
    }

    /**
     * 软删除
     * @param deleteType
     */
    @Override
    public void softDelete(PatrolInspectionType deleteType) {
        deleteType.setDeleteFlag(1);
        baseMapper.updateById(deleteType);
    }

    /**
     * 查类型名字
     */
    @Override
    public String getName(String code){
        LambdaQueryWrapper<PatrolInspectionType> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(PatrolInspectionType::getTypeName)
                .eq(PatrolInspectionType::getOperateCode,code);
        PatrolInspectionType patrolInspectionType = baseMapper.selectOne(queryWrapper);
        if (patrolInspectionType == null) {
            return "";
        }
            return patrolInspectionType.getTypeName();
    }
}
