package com.hzs.system.base.provider;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EApprovalBusiness;
import com.hzs.common.core.enums.EApproveOperation;
import com.hzs.common.core.enums.EControlType;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.domain.system.base.BdNotice;
import com.hzs.system.base.INoticeAuditServiceApi;
import com.hzs.system.base.service.IBdNoticeService;
import com.hzs.system.base.vo.BdNoticeVO;
import com.hzs.system.sys.dto.ApprovalBusinessResultDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 会员钱包审核Doubbo服务
 */
@Slf4j
@DubboService
public class NoticeServiceProvider implements INoticeAuditServiceApi {

    @Autowired
    private IBdNoticeService bdNoticeService;

    @Override
    public R noticeAuditProcess(ApprovalBusinessResultDTO approvalBusinessResultDTO) {
        Boolean isDelete = true;
        //通过/驳回（EApproveOperation）
        Integer approveOperation = approvalBusinessResultDTO.getApproveOperation();
        EApprovalBusiness eApprovalBusiness = approvalBusinessResultDTO.getEApprovalBusiness();
        String businessCode = approvalBusinessResultDTO.getBusinessCode();

        if (StringUtils.isEmpty(businessCode) || null == approveOperation || null == eApprovalBusiness) {
            return R.fail("提交审核缺少参数！");
        }
        try {
            List<BdNoticeVO> bdNoticeList = JSONUtil.toList(approvalBusinessResultDTO.getBusinessData(), BdNoticeVO.class);
            if (CollectionUtil.isNotEmpty(bdNoticeList)) {
                BdNoticeVO bdNotice = bdNoticeList.get(0);
                if (null != bdNotice && null != bdNotice.getPkId()) {
                    //修改公告、站内信信息，审批通过 则原有的公告逻辑删除、新增的更改审批状态
                    if (approveOperation.equals(EApproveOperation.AGREE.getValue())) {
                        bdNoticeService.deleteNoticeById(bdNotice.getPkId(), approvalBusinessResultDTO.getLoginUser().getUserId());
                    } else {
                        //驳回 原有的公告不处理、新增的公告变为修改状态
                    }
                    //删除操作
                    Integer operateType = bdNotice.getOperateType();
                    if (null != operateType && operateType.equals(EControlType.DELETE.getValue())) {
                        isDelete = false;
                    }

                }
            }
            if (isDelete) {
                BdNotice notice = dtoToParam(approvalBusinessResultDTO, approveOperation + 1);
                return R.ok(bdNoticeService.updateNoticeByBatch(notice));
            } else {
                return R.ok();
            }
        } catch (Exception e) {
            //钱包业务发生异常时，提醒审批流程事务回滚
            log.error("审核状态为： {} 的审核出错", approveOperation, e);
            return R.fail("审批公告、站内信流程出错！");
        }
    }

    /**
     * 转业务参数
     *
     * @param approvalBusinessResultDTO 业务审批操作DTO
     * @param approveState              审核状态
     * @return MemberAccountAudit
     */
    private BdNotice dtoToParam(ApprovalBusinessResultDTO approvalBusinessResultDTO, Integer approveState) {
        BdNotice notice = BdNotice.builder()
                .approver(approvalBusinessResultDTO.getLoginUser().getUserId())
                .approveState(approveState)
                .approveTime(approvalBusinessResultDTO.getOperationTime())
                .batch(approvalBusinessResultDTO.getBusinessCode())
                .build();
        notice.setPkModified(approvalBusinessResultDTO.getLoginUser().getUserId());
        return notice;
    }


}
