package com.hzs.ground.product.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.CommonUtil;
import com.hzs.common.domain.scm.th.ThApprovalLog;
import com.hzs.common.domain.scm.th.ThProduct;
import com.hzs.ground.product.mapper.ThProductMapper;
import com.hzs.ground.product.param.ProductInfoParam;
import com.hzs.ground.product.param.ThProductParam;

import com.hzs.ground.product.service.*;
import com.hzs.ground.product.vo.ProductVo;
import com.hzs.system.sys.IApprovalServiceApi;
import com.hzs.system.sys.dto.ApprovalSubmitDTO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务实现类
 */
@Service
public class ThProductServiceImpl extends ServiceImpl<ThProductMapper, ThProduct> implements IThProductService {

    @DubboReference
    IApprovalServiceApi approvalServiceApi;

    @Autowired
    private IThApprovalLogService approvalLogService;

    /**
     * 查询列表
     *
     * @param productInfoParam
     * @return
     */
    @Override
    public List<ProductVo> queryByList(ProductInfoParam productInfoParam) {
        return baseMapper.queryByList(productInfoParam);
    }

    /**
     * 根据id 查询详情
     *
     * @param productId
     * @return
     */
    @Override
    public ProductVo getProductInfoById(Integer productId) {
        return baseMapper.getProductInfoById(productId);
    }


    /**
     * 提交审批
     *
     * @param productParam 抽奖参数
     */
    public Boolean submitApproval(ThProductParam productParam, EApprovalBusiness approvalBusiness) {
        //保存审批日志
        ThApprovalLog approvalLog = saveApprovalLog(productParam, approvalBusiness);
        ProductInfoParam productInfoParam = productParam.getProductInfoParam();


        ApprovalSubmitDTO approvalSubmitDTO = ApprovalSubmitDTO.builder().eApprovalBusiness(approvalBusiness)
                .businessCode(approvalLog.getBusinessCode())
                .businessData(JSONUtil.toJsonStr(productInfoParam))
                .signType(ESignType.getEnumByValue(productParam.getSignType())).userIdList(productParam.getUserIdList())
                .sendIdList(productParam.getSendIdList()).remark(productParam.getRemark()).fileList(productParam.getFileList()).build();
        R<String> submit = approvalServiceApi.submit(approvalSubmitDTO, productParam.getLoginUser());
        if (!submit.isSuccess()) {
            throw new RuntimeException("供应商商品参数提交审批失败！");
        }
        return submit.isSuccess();
    }

    /**
     * 保存活动审批日志信息
     *
     * @param recommendParam   直推赠送参数
     * @param approvalBusiness 审批业务
     * @return boolean
     */
    public ThApprovalLog saveApprovalLog(ThProductParam recommendParam, EApprovalBusiness approvalBusiness) {
        Integer controlType = recommendParam.getControlType();
        String afterData = JSONUtil.toJsonStr(recommendParam.getProductInfoParam());
        String beforeData = controlType.equals(EControlType.SAVE.getValue()) ? JSONUtil.toJsonStr(recommendParam.getProductInfoParam()) : null;
        //业务单号
        String businessCode = CommonUtil.createSerialNumber(EOrderPrefix.SUPPLIER.getValue());
        ThApprovalLog approvalLog = ThApprovalLog.builder().approvalType(approvalBusiness.getValue())
                .beforeData(beforeData).businessCode(businessCode).controltype(controlType)
                .approvalStatus(EApproveRechargeStatus.WAIT.getValue()).afterData(afterData)
                .build();
        approvalLog.setPkCreator(recommendParam.getPkMember());
        approvalLog.setPkCountry(recommendParam.getPkCountry());

        approvalLogService.save(approvalLog);
        return approvalLog;
    }

}
