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

import com.gic.api.base.commons.BasePageInfo;
import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.util.PageHelperUtils;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.haoban.base.api.common.pojo.dto.WebLoginDTO;
import com.gic.haoban.common.utils.AuthWebRequestUtil;
import com.gic.haoban.common.utils.EntityUtil;
import com.gic.haoban.manage.api.dto.OpenStaffDTO;
import com.gic.haoban.manage.api.dto.OpenStaffLicenseDTO;
import com.gic.haoban.manage.api.dto.StaffClerkRelationDTO;
import com.gic.haoban.manage.api.dto.StaffDTO;
import com.gic.haoban.manage.api.dto.chat.GroupChatHmDTO;
import com.gic.haoban.manage.api.qdto.OpenStaffInitQDTO;
import com.gic.haoban.manage.api.qdto.OpenStaffPageQDTO;
import com.gic.haoban.manage.api.service.OpenStaffApiService;
import com.gic.haoban.manage.api.service.StaffApiService;
import com.gic.haoban.manage.api.service.StaffClerkRelationApiService;
import com.gic.haoban.manage.web.qo.open.OpenStaffQO;
import com.gic.haoban.manage.web.vo.chat.GroupChatHmVO;
import com.gic.haoban.manage.web.vo.open.OpenStaffVO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 企微托管
 */
@RestController
@RequestMapping("/qw-open")
public class QywxOpenController {

    @Autowired
    private OpenStaffApiService openStaffApiService ;
    @Autowired
    private StaffApiService staffApiService ;
    @Autowired
    private StaffClerkRelationApiService staffClerkRelationApiService ;

    /**
     * 托管列表
     * @param qo
     * @return
     */
    @RequestMapping("open-staff-page")
    public RestResponse<Page<OpenStaffVO>> page(@RequestBody OpenStaffQO qo,BasePageInfo basePageInfo) {
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        OpenStaffPageQDTO qdto = EntityUtil.changeEntityByJSON(OpenStaffPageQDTO.class,qo) ;
        qdto.setWxEnterpriseId(loginUser.getWxEnterpriseId());
        qdto.setEnterpriseId(loginUser.getEnterpriseId());
        ServiceResponse<Page<OpenStaffDTO>> pageResp = this.openStaffApiService.page(qdto, basePageInfo);
        Page<OpenStaffDTO> page = pageResp.getResult();
        Page<OpenStaffVO> retPage = PageHelperUtils.changePageToCurrentPage(page, OpenStaffVO.class);
        return RestResponse.successResult(retPage);
    }

    /**
     * 托管列表
     * @param qo
     * @return
     */
    @RequestMapping("open-staff-excel")
    public RestResponse<Page<OpenStaffVO>> excel(@RequestBody OpenStaffQO qo) {
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        OpenStaffPageQDTO qdto = EntityUtil.changeEntityByJSON(OpenStaffPageQDTO.class,qo) ;
        qdto.setWxEnterpriseId(loginUser.getWxEnterpriseId());
        qdto.setEnterpriseId(loginUser.getEnterpriseId());
        return RestResponse.successResult(null);
    }

    /**
     * 登录初始化
     * @param staffId
     * @return
     */
    @RequestMapping("login-init")
    public RestResponse<OpenStaffDTO> init(String staffId , @RequestParam(defaultValue = "0") int loginAgainFlag) {
        WebLoginDTO login = AuthWebRequestUtil.getLoginUser();
        String wxEnterpriseId = login.getWxEnterpriseId();
        String enterpriseId = login.getEnterpriseId() ;
        if(StringUtils.isBlank(staffId)) {
            return RestResponse.failure("9999","请先选择成员") ;
        }
        StaffDTO staff = this.staffApiService.selectById(staffId) ;
        if(null == staff || !staff.getWxEnterpriseId().equals(wxEnterpriseId)) {
            return RestResponse.failure("9999","成员不存在") ;
        }
        List<StaffClerkRelationDTO> relationList = this.staffClerkRelationApiService.listByStaffId(wxEnterpriseId,staffId) ;
        if(CollectionUtils.isEmpty(relationList)) {
            return RestResponse.failure("9999","成员未关联导购") ;
        }
        relationList = relationList.stream().filter(o->o.getClerkType() <=1 && o.getEnterpriseId().equals(enterpriseId)).collect(Collectors.toList());
        if(CollectionUtils.isEmpty(relationList)) {
            return RestResponse.failure("9999","成员需关联gic商户下导购") ;
        }
        String checkMsg = this.check(wxEnterpriseId,enterpriseId) ;
        if(null != this.check(wxEnterpriseId,enterpriseId)) {
            return RestResponse.failure("9999",checkMsg) ;
        }
        OpenStaffInitQDTO initQDTO = new OpenStaffInitQDTO() ;
        initQDTO.setWxEnterpriseId(wxEnterpriseId);
        initQDTO.setEnterpriseId(enterpriseId);
        initQDTO.setStaffId(staffId);
        initQDTO.setLoginAgainFlag(loginAgainFlag);
        ServiceResponse<Long> resp = this.openStaffApiService.init(initQDTO) ;
        if(!resp.isSuccess()) {
            return RestResponse.failure("9999",resp.getMessage()) ;
        }
        Long openStaffId = resp.getResult() ;
        return RestResponse.successResult(this.openStaffApiService.getById(openStaffId).getResult());
    }

    /**
     * 获取账号详情
     * @param openStaffId
     * @return
     */
    @RequestMapping("get-open-staff")
    public RestResponse<OpenStaffDTO> getOpenStaff(Long openStaffId) {
        ServiceResponse<OpenStaffDTO> resp = this.openStaffApiService.getById(openStaffId) ;
        if(!resp.isSuccess()) {
            return RestResponse.failure("9999",resp.getMessage()) ;
        }
        return RestResponse.successResult(resp.getResult());
    }

    /**
     * 校验验证码
     * @param openStaffId
     * @param code
     * @return
     */
    @RequestMapping("check-code")
    public RestResponse<OpenStaffDTO> checkCode(Long openStaffId , String code) {
        ServiceResponse<Void> resp = this.openStaffApiService.checkCode(openStaffId,code) ;
        if(!resp.isSuccess()) {
            return RestResponse.failure("9999",resp.getMessage()) ;
        }
        return RestResponse.successResult();
    }

    /**
     * 许可数量查询
     */
    @RequestMapping("license-info")
    public RestResponse<OpenStaffLicenseDTO> licenseInfo() {
        WebLoginDTO login = AuthWebRequestUtil.getLoginUser();
        String wxEnterpriseId = login.getWxEnterpriseId();
        String enterpriseId = login.getEnterpriseId() ;
        ServiceResponse<OpenStaffLicenseDTO> resp = this.openStaffApiService.getLicenseInfo(wxEnterpriseId,enterpriseId) ;
        if(!resp.isSuccess()) {
            return RestResponse.failure("9999",resp.getMessage()) ;
        }
        return RestResponse.successResult(resp.getResult());
    }

    private String check(String wxEnterpriseId , String enterpriseId) {
        return null ;
    }

}
