package com.fs.app.controller;

import com.fs.app.annotation.Login;
import com.fs.app.param.CompanyWxListParam;
import com.fs.common.annotation.Log;
import com.fs.common.core.domain.R;
import com.fs.common.enums.BusinessType;
import com.fs.company.domain.CompanyUser;
import com.fs.company.mapper.CompanyWxAccountMapper;
import com.fs.company.service.ICompanyUserService;
import com.fs.company.service.ICompanyWxClientService;
import com.fs.company.vo.AddWxResultVo;
import com.fs.wxUser.domain.CompanyWxUser;
import com.fs.wxUser.service.ICompanyWxUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 客户Controller
 *
 * @author fs
 * @date 2022-12-21
 */
@AllArgsConstructor
@RestController
@RequestMapping("/app/companyWxUser")
public class CompanyWxUserController extends AppBaseController
{
    private final ICompanyWxUserService companyWxUserService;
    private final ICompanyUserService companyUserService;
    private final ICompanyWxClientService companyWxClientService;
    private final UserController userController;
    private final CompanyWxAccountMapper companyWxAccountMapper;

    // 待添加微信列表
    @GetMapping("/toDayAddWxAccountList")
    @Login
    @Transactional
    public R toDayAddWxAccountList(){
        return R.ok().put("data", companyWxClientService.toDayAddWxAccountList(Long.parseLong(getUserId()), Long.parseLong(getAccountId()), 0));
    }
    // 添加微信回调
    @PostMapping("/addWxResult")
    public R addWxResult(@RequestBody AddWxResultVo vo){
        vo.setAccountId(Long.parseLong(getAccountId()));
        companyWxClientService.addWxResult(vo);
        return R.ok();
    }
    // 加微成功回调接口
    @PostMapping("/addWxTrueResult")
    public R addWxTrueResult(@RequestBody AddWxResultVo vo){
        vo.setAccountId(Long.parseLong(getAccountId()));
        companyWxClientService.addWxTrueResult(vo);
        return R.ok();
    }
    // 添加中的个微列表
    @GetMapping("/addingList")
    public R addingList(){
        return R.ok().put("data", companyWxClientService.toDayAddWxAccountList(Long.parseLong(getUserId()), Long.parseLong(getAccountId()), 2));
    }
    @Login
    @ApiOperation("添加")
    @Log(title = "添加", businessType = BusinessType.UPDATE)
    @PostMapping("/addCompanyWxUser")
    public R addCompanyWxUser(@RequestBody CompanyWxUser param) {
        CompanyWxUser user = companyWxUserService.selectCompanyWxUserByWeixinId(param.getWeixinId());
        if(user==null){
            user=new CompanyWxUser();
            user.setNickName(param.getNickName());
            user.setWeixinId(param.getWeixinId());
            CompanyUser companyUser = companyUserService.selectCompanyUserById(Long.parseLong(getUserId()));
            user.setCompanyUserId(companyUser.getUserId());
            user.setStatus(1);
            user.setCompanyId(companyUser.getCompanyId());
            companyWxUserService.insertCompanyWxUser(user);
            return R.ok();
        }
        else{
            user.setNickName(param.getNickName());
            companyWxUserService.updateCompanyWxUser(user);
            return R.ok();
        }
    }
    @Login
    @ApiOperation("获取员工个微列表")
    @GetMapping("/getCompanyWxList")
    public R getCompanyWxList(CompanyWxListParam param){
        CompanyUser user = companyUserService.selectCompanyUserById(Long.parseLong(getUserId()));
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        CompanyWxUser wxUser = new CompanyWxUser();
        wxUser.setCompanyId(user.getCompanyId());
        wxUser.setCompanyUserId(user.getUserId());
        wxUser.setNickName(param.getKeyword());
        wxUser.setAccountId(Long.parseLong(getAccountId()));
        List<CompanyWxUser> list = companyWxUserService.selectCompanyWxUserList(wxUser);
        PageInfo<CompanyWxUser> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }
    @Login
    @ApiOperation("获取员工个微详情")
    @GetMapping("/getComponyWxDetails")
    public R getComponyWxDetails(Long userId){
        CompanyWxUser companyWxUser = companyWxUserService.selectCompanyWxUserByUserId(userId);
        return R.ok().put("data",companyWxUser);
    }
    @Login
    @ApiOperation("更新个微信息")
    @PostMapping("/updateCompanyWx")
    public R updateCompanyWx(@RequestBody CompanyWxUser wxUser){
        companyWxUserService.updateCompanyWxUser(wxUser);
        return R.ok();
    }
    @Login
    @ApiOperation("获取发送信息")
    @GetMapping("/getMsgList")
    public R getMsg(){
        return R.ok().put("data", companyWxUserService.getMsg(Long.parseLong(getAccountId()), LocalDateTime.now()));
    }
}
