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
     * @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(null) ;
        }
        OpenStaffVO vo = new OpenStaffVO() ;
        OpenStaffDTO dto = resp.getResult();
        String wxEnterpriseId = dto.getWxEnterpriseId() ;
        String enterpriseId = dto.getEnterpriseId() ;
        if(dto.getStatusFlag()==1) {
            if(authFlag==1) {
                return RestResponse.failure("9999","无效授权,账号托管中");
            }
            vo.setStatusFlag(1);
        }else {
            vo.setStatusFlag(2);
            if(authFlag==1) {
                OpenStaffInitQDTO qdto = new OpenStaffInitQDTO() ;
                qdto.setStaffId(staffId);
                qdto.setWxEnterpriseId(wxEnterpriseId);
                qdto.setEnterpriseId(enterpriseId);
                ServiceResponse<Long> codeResp = this.openStaffApiService.init(qdto) ;
                if(null == codeResp.getResult()) {
                    return RestResponse.failure("9999",codeResp.getMessage());
                }
                ServiceResponse<OpenStaffDTO> staffResp =this.openStaffApiService.getById(codeResp.getResult()) ;
                if(null == staffResp.getResult()) {
                    return RestResponse.failure("9999",staffResp.getMessage());
                }
                vo.setQrcode(staffResp.getResult().getQrCode1());
            }
        }
        WxEnterpriseDTO en = this.wxEnterpriseApiService.getOne(wxEnterpriseId) ;
        vo.setCorpName(en.getCorpName());
        StaffDTO staffDTO = this.staffApiService.selectById(staffId) ;
        vo.setStaffName(staffDTO.getStaffName());
        vo.setStaffImage(staffDTO.getHeadImg());
        return RestResponse.successResult(vo);
    }
}
