package com.linkwechat.web.controller.wecom;

import com.linkwechat.common.annotation.Log;
import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.core.controller.BaseController;
import com.linkwechat.common.core.domain.AjaxResult;
import com.linkwechat.common.core.page.TableDataInfo;
import com.linkwechat.common.enums.BusinessType;
import com.linkwechat.common.utils.SecurityUtils;
import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.common.utils.poi.ExcelUtil;
import com.linkwechat.wecom.domain.WeCustomer;
import com.linkwechat.wecom.domain.vo.WeMakeCustomerTag;
import com.linkwechat.wecom.service.IWeCustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 企业微信客户Controller
 *
 * @author ruoyi
 * @date 2020-09-13
 */
@Api(tags = "企业微信客户Controller")
@RestController
@RequestMapping("/wecom/customer")
public class WeCustomerController extends BaseController {

    @Autowired
    @Lazy
    private IWeCustomerService weCustomerService;


    /**
     * 查询企业微信客户列表
     */
    //@PreAuthorize("@ss.hasPermi('customerManage:customer:list')")
    @Log(title = "查询企业微信客户列表", businessType = BusinessType.SELECT)
    @GetMapping("/list")
    @ApiOperation(value = "查询企业微信客户列表", httpMethod = "GET")
    public TableDataInfo<List<WeCustomer>> list(WeCustomer weCustomer) {
        startPage();
        List<WeCustomer> list = weCustomerService.selectWeCustomerList(weCustomer);
        return getDataTable(list);
    }

    /**
     * 根据员工ID获取客户
     *
     * @return
     */
    //  @PreAuthorize("@ss.hasPermi('customerManage:customer:list')")
    @Log(title = "根据员工ID获取客户", businessType = BusinessType.SELECT)
    @ApiOperation(value = "根据员工ID获取客户", httpMethod = "GET")
    @GetMapping("/getCustomersByUserId/{userId}")
    public AjaxResult<List<WeCustomer>> getCustomersByUserId(@PathVariable("userId") String userId) {
        return AjaxResult.success(weCustomerService.getCustomersByUserId(userId));
    }

    /**
     * 导出企业微信客户列表
     */
    //   @PreAuthorize("@ss.hasPermi('wecom:customer:export')")
    @Log(title = "导出企业微信客户列表", businessType = BusinessType.EXPORT)
    @ApiOperation(value = "导出企业微信客户列表", httpMethod = "GET")
    @GetMapping("/export")
    public AjaxResult export(WeCustomer weCustomer) {
        List<WeCustomer> list = weCustomerService.selectWeCustomerList(weCustomer);
        ExcelUtil<WeCustomer> util = new ExcelUtil<WeCustomer>(WeCustomer.class);
        return util.exportExcel(list, "customer");
    }

    /**
     * 根据客户id获取客户详细信息
     */
    //  @PreAuthorize("@ss.hasPermi('customerManage:customer:view')")
    @Log(title = "根据客户id获取客户详细信息", businessType = BusinessType.SELECT)
    @ApiOperation(value = "根据客户id获取客户详细信息", httpMethod = "GET")
    @GetMapping(value = "/{externalUserId}")
    public AjaxResult<WeCustomer> getInfo(@PathVariable("externalUserId") String externalUserId) {
        return AjaxResult.success(weCustomerService.selectWeCustomerByEid(externalUserId));
    }


    /**
     * 修改企业微信客户
     */
    //   @PreAuthorize("@ss.hasPermi('wecom:customer:edit')")
    @Log(title = "修改企业微信客户", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改企业微信客户", httpMethod = "PUT")
    @PutMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody WeCustomer weCustomer) {
        weCustomerService.saveOrUpdate(weCustomer);
        return AjaxResult.success();
    }


    /**
     * 客户同步接口
     *
     * @return
     */
    //   @PreAuthorize("@ss.hasPermi('customerManage:customer:sync')")
    @Log(title = "客户同步接口", businessType = BusinessType.OTHER)
    @ApiOperation(value = "客户同步接口", httpMethod = "GET")
    @GetMapping("/synchWeCustomer")
    public AjaxResult synchWeCustomer() {
        if(StringUtils.isNotEmpty(SecurityUtils.getLoginUser().getUser().getCorpId())){
            return AjaxResult.error("请登陆后重试");
        }
        SecurityContext context = SecurityContextHolder.getContext();
        SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_INHERITABLETHREADLOCAL);
        SecurityContextHolder.setContext(context);
        weCustomerService.synchWeCustomer();
        return AjaxResult.success(WeConstans.SYNCH_TIP);

    }


    /**
     * 客户打标签
     *
     * @param weMakeCustomerTag
     * @return
     */
    //   @PreAuthorize("@ss.hasPermi('customerManage/customer:makeTag')")
    @Log(title = "客户打标签", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "客户打标签", httpMethod = "POST")
    @PostMapping("/makeLabel")
    public AjaxResult makeLabel(@RequestBody WeMakeCustomerTag weMakeCustomerTag) {
        weCustomerService.makeLabel(weMakeCustomerTag);
        return AjaxResult.success();
    }


    /**
     * 移除客户标签
     *
     * @return
     */
    //   @PreAuthorize("@ss.hasPermi('customerManage:customer:removeTag')")
    @Log(title = "移除客户标签", businessType = BusinessType.DELETE)
    @ApiOperation(value = "移除客户标签", httpMethod = "DELETE")
    @DeleteMapping("/removeLabel")
    public AjaxResult removeLabel(@RequestBody WeMakeCustomerTag weMakeCustomerTag) {
        weCustomerService.removeLabel(weMakeCustomerTag);
        return AjaxResult.success();

    }

}
