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.CollectionUtils;
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.DemFireInspectPerson;
import com.usky.fire.mapper.DemFireInspectPersonMapper;
import com.usky.fire.service.DemFireInspectPersonService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 消防检查人员 服务实现类
 * </p>
 *
 * @author JCB
 * @since 2022-11-24
 */
@Service
public class DemFireInspectPersonServiceImpl extends AbstractCrudService<DemFireInspectPersonMapper, DemFireInspectPerson> implements DemFireInspectPersonService {

    @Override
    public CommonPage<DemFireInspectPerson> fireInspectPersonList(String personName, String personPosition, Integer id, Integer pageNum, Integer pageSize) {
        IPage<DemFireInspectPerson> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<DemFireInspectPerson> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like(StringUtils.isNotBlank(personName), DemFireInspectPerson::getPersonName, personName)
                .like(StringUtils.isNotBlank(personPosition), DemFireInspectPerson::getPersonPosition, personPosition)
                .eq(null != id && id != 0, DemFireInspectPerson::getId, id)
                .orderByDesc(DemFireInspectPerson::getId);
        page = this.page(page, queryWrapper);
        return new CommonPage<>(page.getRecords(), page.getTotal(), pageSize, pageNum);
    }

    @Override
    public List<DemFireInspectPerson> fireInspectPersonList(String personName, String personPosition, List<Integer> idList) {
        LambdaQueryWrapper<DemFireInspectPerson> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like(StringUtils.isNotBlank(personName), DemFireInspectPerson::getPersonName, personName)
                .like(StringUtils.isNotBlank(personPosition), DemFireInspectPerson::getPersonPosition, personPosition)
                .in(CollectionUtils.isNotEmpty(idList), DemFireInspectPerson::getId, idList)
                .orderByDesc(DemFireInspectPerson::getId);
        List<DemFireInspectPerson> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public int fireInspectPersonCount(String personName, String personPosition, Integer id) {
        LambdaQueryWrapper<DemFireInspectPerson> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like(StringUtils.isNotBlank(personName), DemFireInspectPerson::getPersonName, personName)
                .like(StringUtils.isNotBlank(personPosition), DemFireInspectPerson::getPersonPosition, personPosition)
                .ne(null != id && id != 0, DemFireInspectPerson::getId, id)
                .orderByDesc(DemFireInspectPerson::getId);
        int count = this.count(queryWrapper);
        return count;
    }

    @Override
    public void addFireInspectPerson(DemFireInspectPerson demFireInspectPerson) {
        int count = this.fireInspectPersonCount(demFireInspectPerson.getPersonName(), demFireInspectPerson.getPersonPosition(), 0);
        if (count > 0) {
            throw new BusinessException("检查员重复，不可添加");
        }
        demFireInspectPerson.setCreator(SecurityUtils.getUsername());
        demFireInspectPerson.setCreateTime(LocalDateTime.now());
        this.save(demFireInspectPerson);
    }

    @Override
    public void updateFireInspectPerson(DemFireInspectPerson demFireInspectPerson) {
        int count = this.fireInspectPersonCount(demFireInspectPerson.getPersonName(), demFireInspectPerson.getPersonPosition(), demFireInspectPerson.getId());
        if (count > 0) {
            throw new BusinessException("检查员重复，不可修改");
        }
        demFireInspectPerson.setUpdatePerson(SecurityUtils.getUsername());
        demFireInspectPerson.setUpdateTime(LocalDateTime.now());
        this.updateById(demFireInspectPerson);
    }

    @Override
    public void delFireInspectPerson(Integer id) {
        this.removeById(id);
    }
}
