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.StringUtils;
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.core.exception.BusinessException;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.fire.domain.DemLargeSecurityManage;
import com.usky.fire.mapper.DemLargeSecurityManageMapper;
import com.usky.fire.service.DemLargeSecurityManageService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 大型安保管理 服务实现类
 * </p>
 *
 * @author JCB
 * @since 2022-10-26
 */
@Service
public class DemLargeSecurityManageServiceImpl extends AbstractCrudService<DemLargeSecurityManageMapper, DemLargeSecurityManage> implements DemLargeSecurityManageService {

    @Override
    public CommonPage<DemLargeSecurityManage> largeSecurityManageList(String securityTipsName, Integer id, Integer pageNum, Integer pageSize) {
        IPage<DemLargeSecurityManage> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<DemLargeSecurityManage> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DemLargeSecurityManage::getDeleteFlag, 0)
                .eq(StringUtils.isNotBlank(securityTipsName), DemLargeSecurityManage::getSecurityTipsName, securityTipsName)
                .eq(id != 0 && id != null, DemLargeSecurityManage::getId, id)
                .orderByDesc(DemLargeSecurityManage::getId);
        page = this.page(page, queryWrapper);
        return new CommonPage<>(page.getRecords(), page.getTotal(), pageSize, pageNum);
    }

    @Override
    public void addLargeSecurityManage(DemLargeSecurityManage demLargeSecurityManage) {
        demLargeSecurityManage.setDeleteFlag(0);
        demLargeSecurityManage.setCreateTime(LocalDateTime.now());
        demLargeSecurityManage.setCreator(SecurityUtils.getUsername());
        this.save(demLargeSecurityManage);
    }

    @Override
    public void updateLargeSecurityManage(DemLargeSecurityManage demLargeSecurityManage) {
        demLargeSecurityManage.setUpdateTime(LocalDateTime.now());
        demLargeSecurityManage.setUpdatePerson(SecurityUtils.getUsername());
        this.updateById(demLargeSecurityManage);
    }

    @Override
    public void delLargeSecurityManage(Integer id) {
        DemLargeSecurityManage demLargeSecurityManage = new DemLargeSecurityManage();
        demLargeSecurityManage.setId(id);
        demLargeSecurityManage.setDeleteFlag(1);
        this.updateById(demLargeSecurityManage);
    }
}
