package com.njzscloud.supervisory.biz.service;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.supervisory.biz.constant.AuditStatus;
import com.njzscloud.supervisory.biz.constant.BizObj;
import com.njzscloud.supervisory.biz.mapper.BizProjectMapper;
import com.njzscloud.supervisory.biz.pojo.entity.BizProjectEntity;
import com.njzscloud.supervisory.biz.pojo.result.SearchBizProjectResult;
import com.njzscloud.supervisory.sys.auth.pojo.result.MyResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 项目信息
 */
@Slf4j
@Service
public class BizProjectService extends ServiceImpl<BizProjectMapper, BizProjectEntity> implements IService<BizProjectEntity> {

    /**
     * 新增
     */
    // @Transactional(rollbackFor = Exception.class)
    public void add(BizProjectEntity bizProjectEntity) {
/*         Long userId = SecurityUtil.currentUserId();
        Long otherCompanyId = bizProjectEntity.getOtherCompanyId();
        Integer auditStatus = baseMapper.companyAudit(Wrappers.query()
                .eq("id", otherCompanyId)
                .eq("audit_status", AuditStatus.TongGuo)
                .eq("deleted", 0));
        Assert.isTrue(auditStatus == 1, "合作方公司未审核");

        auditStatus = baseMapper.companyAudit(Wrappers.query()
                .eq("user_id", userId)
                .eq("audit_status", AuditStatus.TongGuo)
                .eq("deleted", 0));
        Assert.isTrue(auditStatus == 1, "当前公司未审核"); */
        this.save(bizProjectEntity.setAuditStatus(AuditStatus.DaiShenHe));
    }

    /**
     * 修改
     */
    public void modify(BizProjectEntity bizProjectEntity) {
        this.updateById(bizProjectEntity);
    }

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

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

    /**
     * 分页查询
     */
    public PageResult<SearchBizProjectResult> paging(PageParam pageParam, BizProjectEntity bizProjectEntity) {
        AuditStatus auditStatus = bizProjectEntity.getAuditStatus();
        String aStatus = "";
        if (null != auditStatus) {
            aStatus = auditStatus.getVal();
        }
        MyResult userDetail = SecurityUtil.loginUser();
        BizObj bizObj = userDetail.getBizObj();
        QueryWrapper<BizProjectEntity> ew = Wrappers.<BizProjectEntity>query()
                .like(StrUtil.isNotBlank(bizProjectEntity.getProjectName()), "a.project_name", bizProjectEntity.getProjectName())
                .eq(StrUtil.isNotBlank(aStatus), "a.audit_status", aStatus).eq("a.deleted", 0);

        if (bizObj != null && bizObj.isTrans()) {
            ew.eq("a.user_id", userDetail.getUserId());
        } else if (bizObj != null && bizObj.isWaste()) {
            ew.eq("a.other_user_id", userDetail.getUserId());
        }

        return PageResult.of(baseMapper.paging(pageParam.toPage(), ew));

    }

    @Transactional(rollbackFor = Exception.class)
    public void audit(Long id, AuditStatus auditStatus, String auditMemo) {
        Assert.isTrue(auditStatus == AuditStatus.TongGuo || auditStatus == AuditStatus.BoHui, "错误的审核状态");
        BizProjectEntity oldData = getById(id);
        if (oldData == null) {
            throw Exceptions.exception("未找到要审核的数据");
        }
        if (oldData.getAuditStatus() != AuditStatus.DaiShenHe) {
            throw Exceptions.exception("当前状态不能审核");
        }
        boolean updated = update(Wrappers.<BizProjectEntity>lambdaUpdate()
                .set(BizProjectEntity::getAuditStatus, auditStatus)
                .set(BizProjectEntity::getAuditMemo, auditMemo)
                .eq(BizProjectEntity::getId, id)
                .eq(BizProjectEntity::getAuditStatus, AuditStatus.DaiShenHe));
        if (!updated) {
            throw Exceptions.exception("数据状态已改变，无法审核");
        }
    }

    public PageResult<SearchBizProjectResult> pagingTrans(PageParam pageParam, BizProjectEntity bizProjectEntity) {
        MyResult userDetail = SecurityUtil.loginUser();
        BizObj bizObj = userDetail.getBizObj();
        Assert.isTrue(bizObj != null && bizObj.isWaste(), () -> Exceptions.exception("非产废单位或个人，不能查询合作的清运公司"));

        Long userId = userDetail.getUserId();
        return PageResult.of(baseMapper.paging(pageParam.toPage(), Wrappers.<BizProjectEntity>query()
                .eq("a.deleted", 0)
                .eq("a.audit_status", AuditStatus.TongGuo.getVal())
                .eq("b.user_id", userId)
                .like(StrUtil.isNotBlank(bizProjectEntity.getProjectName()), "a.project_name", bizProjectEntity.getProjectName())
        ));
    }

    public void cancel(Long id) {
        BizProjectEntity oldData = getById(id);
        if (oldData == null) {
            throw Exceptions.exception("未找到要撤销的数据");
        }
        update(Wrappers.<BizProjectEntity>lambdaUpdate()
                .set(BizProjectEntity::getAuditStatus, AuditStatus.YiCheXiao)
                .eq(BizProjectEntity::getId, id)
        );
    }
}
