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.biz.pojo.result.SearchAuditConfigResult;
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;

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


        String area = bizAuditConfigEntity.getArea();
        Assert.isTrue(StrUtil.isNotBlank(area), () -> Exceptions.clierr("区县不能为空"));
        String areaName = baseMapper.getAreaName(area);
        Assert.isTrue(StrUtil.isNotBlank(areaName), () -> Exceptions.clierr("区县名称不能为空"));
        bizAuditConfigEntity.setAreaName(areaName);

        String areaRole = bizAuditConfigEntity.getAreaRole();
        if (StrUtil.isNotBlank(areaRole)) {
        Assert.isTrue(StrUtil.isNotBlank(bizAuditConfigEntity.getCityRole()), () -> Exceptions.clierr("市级审核员不能为空"));
        }
        Assert.isTrue(StrUtil.isNotBlank(bizAuditConfigEntity.getCityRole()), () -> Exceptions.clierr("市级审核员不能为空"));
        this.save(bizAuditConfigEntity);
    }

    /**
     * 修改
     */
    @Transactional(rollbackFor = Exception.class)
    public void modify(BizAuditConfigEntity bizAuditConfigEntity) {
        Long id = bizAuditConfigEntity.getId();
        BizAuditConfigEntity entity = this.getById(id);
        Assert.notNull(entity, () -> Exceptions.clierr("数据不存在，无法修改"));
        String cityRole = bizAuditConfigEntity.getCityRole();
        String areaRole = bizAuditConfigEntity.getAreaRole();
        baseMapper.modify(id, cityRole, areaRole);
    }

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

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

    /**
     * 分页查询
     */
    public PageResult<SearchAuditConfigResult> paging(PageParam pageParam, BizAuditConfigEntity bizAuditConfigEntity) {
        String areaName = bizAuditConfigEntity.getAreaName();
        return PageResult.of(baseMapper.paging(pageParam.toPage(), Wrappers.<BizAuditConfigEntity>query()
                .like(StrUtil.isNotBlank(areaName), "a.area_name", areaName)
        ));
    }

}
