
package com.tmerclub.cloud.platform.controller.platform;

import cn.hutool.core.collection.CollUtil;
import com.tmerclub.cloud.api.feign.SearchSpuFeignClient;
import com.tmerclub.cloud.common.constant.ConfigNameConstant;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.constant.DistributionAudit;
import com.tmerclub.cloud.common.product.vo.search.SpuSearchVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.util.Json;
import com.tmerclub.cloud.platform.dto.DistributionConfigDTO;
import com.tmerclub.cloud.platform.dto.DistributionRecruitConfigDTO;
import com.tmerclub.cloud.platform.model.SysConfig;
import com.tmerclub.cloud.platform.service.SysConfigService;
import com.tmerclub.cloud.platform.vo.DistributionConfigVO;
import com.tmerclub.cloud.platform.vo.DistributionRecruitConfigVO;
import jakarta.validation.Valid;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author cl
 * @date 2021-08-06 14:18:03
 */
@RestController
@RequestMapping("/p/distribution_config")
public class DistributionConfigController {
    @Autowired
    private SysConfigService sysConfigService;
    @DubboReference
    private SearchSpuFeignClient searchSpuFeignClient;

    /**
     * 获取分销配置信息
     */
    @GetMapping("/info")
    public ServerResponseEntity<DistributionConfigVO> info() {
        DistributionConfigVO distributionConfigVO = sysConfigService.getSysConfigObject(Constant.DISTRIBUTION_CONFIG, DistributionConfigVO.class);
        List<Long> spuIdList = distributionConfigVO.getSpuIdList();
        if (CollUtil.isEmpty(spuIdList)) {
            return ServerResponseEntity.success(distributionConfigVO);
        }
        ServerResponseEntity<List<SpuSearchVO>> spuResponse = searchSpuFeignClient.listSpuBySpuIds(spuIdList);
        if (spuResponse.isSuccess()) {
            distributionConfigVO.setSpuVOList(spuResponse.getData());
        }
        return ServerResponseEntity.success(distributionConfigVO);
    }

    /**
     * 获取分销推广配置信息
     */
    @GetMapping("/recruit_info")
    public ServerResponseEntity<DistributionRecruitConfigVO> recruitInfo() {
        DistributionRecruitConfigVO recruitConfigVO = sysConfigService.getSysConfigObject(Constant.DISTRIBUTION_RECRUIT_CONFIG, DistributionRecruitConfigVO.class);
        return ServerResponseEntity.success(recruitConfigVO);
    }

    /**
     * 保存分销配置
     */
    @PostMapping
    public ServerResponseEntity<Void> save(@RequestBody @Valid DistributionConfigDTO distributionConfigDTO) {
        //提现发放方式： 0.无需审核直接发放，1.审核后系统发放，2.审核后人工发放 默认：审核后人工发放
        if (Objects.isNull(distributionConfigDTO.getWithdrawal())) {
            distributionConfigDTO.setWithdrawal(DistributionAudit.TWithdrawals_TWO.value());
        }
        SysConfig config = new SysConfig();
        String paramValue = Json.toJsonString(distributionConfigDTO);
        config.setParamKey(Constant.DISTRIBUTION_CONFIG);
        config.setParamValue(paramValue);
        config.setRemark(ConfigNameConstant.DISTRIBUTION_REMARKS);
        sysConfigService.saveOrUpdateSysConfig(config);
        return ServerResponseEntity.success();
    }

    /**
     * 保存分销配置
     */
    @PostMapping("/recruit")
    public ServerResponseEntity<Void> saveRecruit(@RequestBody @Valid DistributionRecruitConfigDTO distributionRecruitConfigDTO) {
        SysConfig config = new SysConfig();
        String paramValue = Json.toJsonString(distributionRecruitConfigDTO);
        config.setParamKey(Constant.DISTRIBUTION_RECRUIT_CONFIG);
        config.setParamValue(paramValue);
        config.setRemark(ConfigNameConstant.DISTRIBUTION_RECRUIT_REMARKS);
        sysConfigService.saveOrUpdateSysConfig(config);
        return ServerResponseEntity.success();
    }


}
