package com.njzscloud.dispose.cst.org.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.common.security.util.SecurityUtil;
import com.njzscloud.dispose.cst.customer.constant.IdentityCategory;
import com.njzscloud.dispose.cst.org.constant.ApplyStatus;
import com.njzscloud.dispose.cst.org.mapper.OrgApplyMapper;
import com.njzscloud.dispose.cst.org.pojo.entity.OrgApplyEntity;
import com.njzscloud.dispose.cst.org.pojo.param.SearchOrgApplyParam;
import com.njzscloud.dispose.cst.org.pojo.result.SearchOrgApplyResult;
import com.njzscloud.dispose.sys.auth.pojo.result.IdentityInfo;
import com.njzscloud.dispose.sys.auth.pojo.result.MyResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 加入组织申请
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class OrgApplyService extends ServiceImpl<OrgApplyMapper, OrgApplyEntity> implements IService<OrgApplyEntity> {

    /**
     * 新增
     */
    public void add(OrgApplyEntity orgApplyEntity) {
        this.save(orgApplyEntity);
    }

    /**
     * 修改
     */
    public void modify(OrgApplyEntity orgApplyEntity) {
        this.updateById(orgApplyEntity);
    }

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

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

    /**
     * 分页查询
     */
    public PageResult<SearchOrgApplyResult> paging(PageParam pageParam, SearchOrgApplyParam searchOrgApplyParam) {
        LocalDateTime applyStartTime = searchOrgApplyParam.getApplyStartTime();
        LocalDateTime applyEndTime = searchOrgApplyParam.getApplyEndTime();
        ApplyStatus applyStatus = searchOrgApplyParam.getApplyStatus();
        String customerName = searchOrgApplyParam.getCustomerName();
        String customerPhone = searchOrgApplyParam.getCustomerPhone();
        IdentityCategory identityCategory = searchOrgApplyParam.getIdentityCategory();
        MyResult myResult = SecurityUtil.loginUser();
        Long orgId = null;
        if (!SecurityUtil.isAdmin()) {
            IdentityInfo identityInfo = myResult.currentIdentity();
            Assert.notNull(identityInfo, () -> Exceptions.error("仅企业管理员可访问"));
            Assert.isTrue(identityInfo.getManager(), () -> Exceptions.error("仅企业管理员可访问"));
            orgId = identityInfo.getOrgId();
        }

        return PageResult.of(baseMapper.paging(pageParam.toPage(), Wrappers.query()
            .ge(orgId != null, "a.org_id", orgId)
            .ge(applyStartTime != null, "a.apply_time", applyStartTime)
            .le(applyStartTime != null, "a.apply_time", applyEndTime)
            .eq(applyStatus != null, "a.apply_status", applyStatus)
            .eq(identityCategory != null, "b.identity_category", identityCategory)
            .eq(StrUtil.isNotBlank(customerName), "b.customer_name", customerName)
            .eq(StrUtil.isNotBlank(customerPhone), "b.phone", customerPhone)
        ));
    }
}
