package com.fs.qw.controller;

import java.util.List;

import com.fs.common.core.domain.R;
import com.fs.common.utils.ServletUtils;
import com.fs.company.domain.CompanyUser;
import com.fs.company.service.ICompanyUserService;
import com.fs.core.security.LoginUser;
import com.fs.core.web.service.TokenService;
import com.fs.qw.param.QwAccountParam;
import com.fs.qw.vo.QwAccountListVO;
import com.github.pagehelper.util.StringUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.enums.BusinessType;
import com.fs.qw.domain.QwAccount;
import com.fs.qw.service.IQwAccountService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 企微功能账号管理Controller
 *
 * @author fs
 * @date 2023-08-23
 */
@RestController
@RequestMapping("/qw/account")
public class QwAccountController extends BaseController
{
    @Autowired
    private IQwAccountService qwAccountService;

    @Autowired
    private ICompanyUserService userService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询企微功能账号管理列表
     */
    @PreAuthorize("@ss.hasPermi('qw:account:list')")
    @GetMapping("/list")
    public TableDataInfo list(QwAccountParam param)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        CompanyUser companyUser = userService.selectCompanyUserById(loginUser.getUser().getUserId());
        if (companyUser.isAdmin()){
            param.setCompanyId(loginUser.getCompany().getCompanyId());
        }else {
            param.setCompanyUserId(loginUser.getUser().getUserId());
        }
        startPage();
        List<QwAccountListVO> list = qwAccountService.selectQwAccounts(param);
        return getDataTable(list);
    }

    /**
     * 导出企微功能账号管理列表
     */
    @PreAuthorize("@ss.hasPermi('qw:account:export')")
    @Log(title = "企微功能账号管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(QwAccount qwAccount)
    {
        List<QwAccount> list = qwAccountService.selectQwAccountList(qwAccount);
        ExcelUtil<QwAccount> util = new ExcelUtil<QwAccount>(QwAccount.class);
        return util.exportExcel(list, "account");
    }

    /**
     * 获取企微功能账号管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('qw:account:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(qwAccountService.selectQwAccountById(id));
    }

    /**
     * 新增企微功能账号管理
     */
    @PreAuthorize("@ss.hasPermi('qw:account:add')")
    @Log(title = "企微功能账号管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody QwAccount qwAccount)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        qwAccount.setCompanyId(loginUser.getCompany().getCompanyId());
        qwAccount.setCompanyUserId(loginUser.getUser().getUserId());
        qwAccount.setDeptId(loginUser.getUser().getDeptId());
        int count  = qwAccountService.selectAccountCount(qwAccount.getAccount());
        if (count == 0){
            qwAccountService.insertQwAccount(qwAccount);
            return R.ok();
        }else {
            return R.error("账号已存在，请勿重复添加");
        }
    }

    /**
     * 修改企微功能账号管理
     */
    @PreAuthorize("@ss.hasPermi('qw:account:edit')")
    @Log(title = "企微功能账号管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QwAccount qwAccount)
    {
        return toAjax(qwAccountService.updateQwAccount(qwAccount));
    }

    /**
     * 删除企微功能账号管理
     */
    @PreAuthorize("@ss.hasPermi('qw:account:remove')")
    @Log(title = "企微功能账号管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(qwAccountService.deleteQwAccountByIds(ids));
    }


    @GetMapping("/getDeviceId/{account}")
    public R getDeviceIdByAccount(@PathVariable("account") String account)
    {
        String deviceId = qwAccountService.getDeviceIdByAccount(account);
        if(StringUtil.isEmpty(deviceId)){
            return R.error("请检查账号是否正确");
        }
        return R.ok().put("deviceId",deviceId);
    }

    @GetMapping("/editDeviceId/{id}")
    public R editDeviceId(@PathVariable("id") Long id){
        QwAccount qwAccount = qwAccountService.selectQwAccountById(id);
        if (qwAccount!=null){
            qwAccountService.updateDevice(qwAccount);
        }
        return R.ok();
    }

}
