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.course.param.FsCourseListBySidebarParam;
import com.fs.qw.domain.QwExternalContactInfo;
import com.fs.qw.domain.QwUser;
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 com.fs.qw.vo.QwExternalListByHeavyVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
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.List;
import java.util.Map;

@Slf4j
@Api(tags = "企微会员相关接口")
@RestController
@RequestMapping("/apis/app/qw/user")
public class ApisQwUserController 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不能为空!");
        }
//        QwExternalContact qwExternalContact = qwExternalContactService.selectQwExternalContactById(qwExternalContactId);
        QwExternalContactInfo contactInfo = qwExternalContactInfoService.selectQwExternalContactInfoByExternalContactId(qwExternalContactId);
        if (contactInfo==null){

            contactInfo = new QwExternalContactInfo();
            contactInfo.setExternalContactId(qwExternalContactId);
            qwExternalContactInfoService.insertQwExternalContactInfo(contactInfo);

        }

//        return R.ok().put("data",qwExternalContact).put("moreInfo",contactInfo);
        return R.ok().put("moreInfo",contactInfo);
    }

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

    /**
    * 获取客户是否加了其他的销售(重粉)
    */
    @PostMapping("/getQwExternalListByHeavy")
    @ApiOperation("获取客户是否加了其他的销售")
    public R getQwExternalListByHeavy(@RequestBody FsCourseListBySidebarParam param){
        QwUser qwUser = qwExternalContactService.getQwUserByRedis(param.getCorpId().trim(),param.getQwUserId().trim());

        if (qwUser == null) {
            return R.error("未查询到企业微信账号信息！请重试");
        }

        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<QwExternalListByHeavyVO> qwExternalListByHeavy = qwExternalContactService.getQwExternalListByHeavy(param);
        PageInfo<QwExternalListByHeavyVO> result = new PageInfo<>(qwExternalListByHeavy);
        return R.ok().put("data", result);
    }
}
