package com.fs.app.controller;

import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.R;
import com.fs.common.exception.CustomException;
import com.fs.qw.domain.QwExternalContactInfo;
import com.fs.qw.param.ExternalContactDetailsParam;
import com.fs.qw.service.IQwExternalContactInfoService;
import com.fs.qw.service.IQwExternalContactService;
import com.fs.qw.vo.ExternalContactDetailsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Api(tags = "企微会员相关接口")
@RestController
@RequestMapping("/app/qw/user")
public class QwUserController extends BaseController {

    @Autowired
    private IQwExternalContactService qwExternalContactService;

    @Autowired
    private IQwExternalContactInfoService qwExternalContactInfoService;

    @GetMapping("/details")
    @ApiOperation("会员看课详情")
    public R getUserDetails(@ApiParam(value = "外部联系人id", required = true) @RequestParam Long contactId,
                            @ApiParam(value = "时间tab，不传表示查询全部，分别是：今天、昨天、前天、近七天", required = true) String dateTag) {
        ExternalContactDetailsParam param = new ExternalContactDetailsParam();
        param.setUserId(getUserId());
        param.setContactId(contactId);
        param.setDateTag(dateTag);
        ExternalContactDetailsVO userDetails = qwExternalContactService.getUserDetails(param);
        Map<String, Object> map = new HashMap<>();
        map.put("userDetails", userDetails);
        return R.ok(map);
    }


    @GetMapping("/getQwUserInfo")
    @ApiOperation("获取企微用户信息")
    public R getQwUserInfo(@RequestParam(value = "qwExternalContactId",required = false) Long qwExternalContactId){
        if(qwExternalContactId == null) {
            throw new CustomException("企微外部联系人id不能为空!");
        }

        QwExternalContactInfo qwExternalContactInfo = qwExternalContactInfoService.selectQwExternalContactInfoByExternalContactId(qwExternalContactId);
        return R.ok().put("data",qwExternalContactService.selectQwExternalContactById(qwExternalContactId)).put("moreInfo",qwExternalContactInfo);
    }

    @PostMapping("/updateQwUserInfo")
    @ApiOperation("更新企微用户信息")
    public R updateQwUserInfo(@RequestBody QwExternalContactInfo qwExternalContactInfo){
        if(qwExternalContactInfo.getExternalContactId() == null) {
            throw new CustomException("企微外部联系人id不能为空!");
        }
        qwExternalContactInfoService.updateQwExternalContactInfoByExternalContactId(qwExternalContactInfo);
        return R.ok();
    }

}
