package com.gic.haoban.manage.web.controller;

import com.gic.api.base.commons.Page;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.haoban.manage.api.dto.QywxErrorLogDTO;
import com.gic.haoban.manage.api.dto.WxEnterpriseDTO;
import com.gic.haoban.manage.api.dto.fee.HaobanQywxFeeDTO;
import com.gic.haoban.manage.api.dto.fee.HaobanQywxFeeDetailDTO;
import com.gic.haoban.manage.api.dto.qdto.QywxErrorLogListQDTO;
import com.gic.haoban.manage.api.dto.qdto.QywxFeeListQDTO;
import com.gic.haoban.manage.api.service.QywxErrorLogApiService;
import com.gic.haoban.manage.api.service.WxEnterpriseApiService;
import com.gic.haoban.manage.api.service.fee.HaobanQywxFeeApiService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author mozhu
 * @date 2022/4/19 14:27
 */
@RestController
@RequestMapping("/qywx")
public class QywxManageController extends WebBaseController {

    @Autowired
    private QywxErrorLogApiService qywxErrorLogApiService;
    @Autowired
    private HaobanQywxFeeApiService haobanQywxFeeApiService;
    @Autowired
    private WxEnterpriseApiService wxEnterpriseApiService;
    /**
     * 企微报错列表
     *
     * @param qywxErrorLogListQDTO
     * @return
     */
    @RequestMapping(value = "qywx-error-list", method = RequestMethod.POST)
    public RestResponse qywxErrorList(QywxErrorLogListQDTO qywxErrorLogListQDTO) {
        Page<QywxErrorLogDTO> qywxErrorLogDTOPage = qywxErrorLogApiService.queryListByPage(qywxErrorLogListQDTO);
        return RestResponse.successResult(qywxErrorLogDTOPage);
    }


    /**
     * 接口许可拦截管理列表
     *
     * @param qywxFeeListQDTO
     * @return
     */
    @RequestMapping(value = "qywx-fee-list", method = RequestMethod.POST)
    public RestResponse qywxFeeList(@RequestBody QywxFeeListQDTO qywxFeeListQDTO) {
        Page<HaobanQywxFeeDTO> listPage = haobanQywxFeeApiService.listPage(qywxFeeListQDTO);
        List<HaobanQywxFeeDTO> result = listPage.getResult();
        if (CollectionUtils.isNotEmpty(result)) {
            for (HaobanQywxFeeDTO haobanQywxFeeDTO : result) {
                String wxEnterpriseId = haobanQywxFeeDTO.getWxEnterpriseId();
                WxEnterpriseDTO wxEnterpriseDTO = wxEnterpriseApiService.getOne(wxEnterpriseId);
                if (wxEnterpriseDTO != null) {
                    haobanQywxFeeDTO.setAutoActiveFlag(wxEnterpriseDTO.getAutoActiveFlag());
                }
                List<HaobanQywxFeeDetailDTO> haobanQywxFeeDetailDTOS = haobanQywxFeeApiService.feeDetail(wxEnterpriseId);
                haobanQywxFeeDTO.setDetailNum(CollectionUtils.isEmpty(haobanQywxFeeDetailDTOS) ? 0 : haobanQywxFeeDetailDTOS.size());
            }
            listPage.setResult(result);
        }
        return RestResponse.successResult(listPage);
    }

    /**
     * 拦截查看详情
     *
     * @param wxEnterpriseId
     * @return
     */
    @RequestMapping(value = "qywx-fee-detail", method = RequestMethod.GET)
    public RestResponse qywxFeeDetail(String wxEnterpriseId) {
        List<HaobanQywxFeeDetailDTO> haobanQywxFeeDetailDTOS = haobanQywxFeeApiService.feeDetail(wxEnterpriseId);
        if (CollectionUtils.isEmpty(haobanQywxFeeDetailDTOS)) {
            return RestResponse.successResult();
        }
        return RestResponse.successResult(haobanQywxFeeDetailDTOS);
    }

}
