package com.njzscloud.supervisory.biz.service;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njzscloud.common.core.ex.Exceptions;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.supervisory.biz.mapper.BizAuditConfigMapper;
import com.njzscloud.supervisory.biz.pojo.entity.BizAuditConfigEntity;
import com.njzscloud.supervisory.config.AppProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 审核配置
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class BizAuditConfigService extends ServiceImpl<BizAuditConfigMapper, BizAuditConfigEntity> implements IService<BizAuditConfigEntity> {
    private final AppProperties appProperties;

    /**
     * 新增
     */
    public void add(BizAuditConfigEntity bizAuditConfigEntity) {
        AppProperties.DefaultPlace defaultPlace = appProperties.getDefaultPlace();
        bizAuditConfigEntity.setProvince(defaultPlace.getProvince())
                .setCity(defaultPlace.getCity())
                .setProvinceName(defaultPlace.getProvinceName())
                .setCityName(defaultPlace.getCityName());


        Assert.isTrue(StrUtil.isNotBlank(bizAuditConfigEntity.getArea()), () -> Exceptions.clierr("区县不能为空"));
        Assert.isTrue(StrUtil.isNotBlank(bizAuditConfigEntity.getAreaName()), () -> Exceptions.clierr("区县名称不能为空"));
        Assert.isTrue(StrUtil.isNotBlank(bizAuditConfigEntity.getCityRole()), () -> Exceptions.clierr("市级审核员不能为空"));
        Assert.isTrue(StrUtil.isNotBlank(bizAuditConfigEntity.getCityRole()), () -> Exceptions.clierr("市级审核员不能为空"));
        this.save(bizAuditConfigEntity);
    }

    /**
     * 修改
     */
    public void modify(BizAuditConfigEntity bizAuditConfigEntity) {
        this.updateById(bizAuditConfigEntity);
    }

    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    public void del(List<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 详情
     */
    public BizAuditConfigEntity detail(Long id) {
        return this.getById(id);
    }

    /**
     * 分页查询
     */
    public PageResult<BizAuditConfigEntity> paging(PageParam pageParam, BizAuditConfigEntity bizAuditConfigEntity) {
        return PageResult.of(this.page(pageParam.toPage(), Wrappers.<BizAuditConfigEntity>query(bizAuditConfigEntity)));
    }

}
