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

import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.haoban.manage.api.dto.*;
import com.gic.haoban.manage.api.qdto.OpenStaffInitQDTO;
import com.gic.haoban.manage.api.service.OpenStaffApiService;
import com.gic.haoban.manage.api.service.StaffApiService;
import com.gic.haoban.manage.api.service.WxEnterpriseApiService;
import com.gic.haoban.manage.web.vo.OpenStaffVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 企微托管-好办小程序
 */
@RestController
@RequestMapping("/qw-open")
public class QywxOpenController {

    @Autowired
    private OpenStaffApiService openStaffApiService ;
    @Autowired
    private WxEnterpriseApiService wxEnterpriseApiService ;
    @Autowired
    private StaffApiService  staffApiService ;

    /**
     * 获取详情
     * @param staffId
     * @param authFlag  1授权 0查询
     * @return
     */
    @RequestMapping("get-open-staff")
    public RestResponse<OpenStaffVO> getOpenStaff(String staffId , @RequestParam(defaultValue = "0") int authFlag) {
        ServiceResponse<OpenStaffDTO> resp = this.openStaffApiService.getByStaffId(staffId) ;
        if(!resp.isSuccess()) {
            return RestResponse.successResult();
        }
        OpenStaffVO vo = new OpenStaffVO() ;
        OpenStaffDTO dto = resp.getResult();
        String wxEnterpriseId = dto.getWxEnterpriseId() ;
        String enterpriseId = dto.getEnterpriseId() ;
        WxEnterpriseDTO en = this.wxEnterpriseApiService.getOne(wxEnterpriseId) ;
        vo.setCorpName(en.getCorpName());
        StaffDTO staffDTO = this.staffApiService.selectById(staffId) ;
        vo.setStaffName(staffDTO.getStaffName());
        vo.setStaffImage(staffDTO.getHeadImg());
        if(dto.getStatusFlag()==1) {
            if(authFlag==1) {
                //return RestResponse.failure("9999","无需授权,账号托管中");
                vo.setStatusFlag(1);
                return RestResponse.successResult(vo);
            }
            vo.setStatusFlag(1);
        }else {
            vo.setStatusFlag(2);
            if(authFlag==1) {
                OpenStaffInitQDTO qdto = new OpenStaffInitQDTO() ;
                qdto.setStaffId(staffId);
                qdto.setWxEnterpriseId(wxEnterpriseId);
                qdto.setEnterpriseId(enterpriseId);
                ServiceResponse<OpenStaffDTO> codeResp = this.openStaffApiService.init(qdto) ;
                if(null == codeResp.getResult()) {
                    return RestResponse.failure("9999",codeResp.getMessage());
                }
                vo.setQrcode(codeResp.getResult().getQrCode1());
                vo.setQrcodeData(codeResp.getResult().getQrcodeData());
                vo.setStatusFlag(codeResp.getResult().getStatusFlag());
            }
        }
        return RestResponse.successResult(vo);
    }
}
