package cn.iocoder.yudao.module.basic.controller.app.workersinfo;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.basic.controller.admin.workersinfo.vo.WorkersInfoRespVO;
import cn.iocoder.yudao.module.basic.controller.app.workersinfo.vo.Code2SessionReqVO;
import cn.iocoder.yudao.module.basic.controller.app.workersinfo.vo.VerifyReqVO;
import cn.iocoder.yudao.module.basic.convert.workersinfo.WorkersInfoConvert;
import cn.iocoder.yudao.module.basic.service.workersinfo.WorkersInfoService;
import cn.iocoder.yudao.module.basic.util.WXUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "APP移动端 - 员工")
@RestController
@RequestMapping("/basic/workers-info")
@Validated
public class WorkersInfoAppController {

    @Resource
    private WorkersInfoService workersInfoService;

    @GetMapping("/getWorkersInfo")
    @PermitAll // 无需登陆
    @ApiOperation("通过商户ID和openid获得员工信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "商户ID", required = true, example = "1", dataTypeClass = Long.class),
        @ApiImplicitParam(name = "openid", value = "openid", required = true, example = "ss65sd5tt", dataTypeClass = String.class)
    })
    public CommonResult<WorkersInfoRespVO> getWorkersInfo(@RequestParam("id") Long id, @RequestParam("openid") String openid) {
        return CommonResult.success(WorkersInfoConvert.INSTANCE.convert(workersInfoService.getWorkersInfo(id, openid)));
    }

    @GetMapping("/getByBusinessInfoId")
    @PermitAll // 无需登陆
    @ApiOperation("通过商户ID获得员工信息")
    @ApiImplicitParam(name = "id", value = "商户ID", required = true, example = "1", dataTypeClass = Long.class)
    public CommonResult<List<WorkersInfoRespVO>> getByBusinessInfoId(@RequestParam("id") Long id) {
        return CommonResult.success(WorkersInfoConvert.INSTANCE.convertList(workersInfoService.findByBelongBusinessId(id)));
    }

    @GetMapping("/getAccessToken")
    @PermitAll // 无需登陆
    @ApiOperation("获得access_token")
    public CommonResult<String> getAccessToken() {
        return CommonResult.success(WXUtils.getAccessToken());
    }

    @GetMapping("/getPhoneNumber")
    @PermitAll // 无需登陆
    @ApiOperation("获得手机号码")
    @ApiImplicitParam(name = "code", value = "手机号获取凭证", required = true, example = "e31968a7f94cc5ee25fafc2aef2773f0bb8c3937b22520eb8ee345274d00c144", dataTypeClass = String.class)
    public CommonResult<String> getPhoneNumber(@RequestParam("code") String code) {
        return CommonResult.success(WXUtils.getPhoneNumber(code));
    }

    @GetMapping("/code2Session")
    @PermitAll // 无需登陆
    @ApiOperation("登陆并绑定,返回openid")
    public CommonResult<String> code2Session(@Valid Code2SessionReqVO reqVO) {
        return CommonResult.success(workersInfoService.bindWXToWorkers(reqVO));
    }

    @GetMapping("/getByOpenId")
    @PermitAll // 无需登陆
    @ApiOperation(" 根据openid获得员工， 如果已绑定openid，返回员工信息，否则返回null")
    public CommonResult<WorkersInfoRespVO> getByOpenId(@Valid VerifyReqVO reqVO) {
        return CommonResult.success(WorkersInfoConvert.INSTANCE.convert(workersInfoService.getWorkersInfoByOpenId(reqVO)));
    }
}
