package org.dromara.work.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.work.domain.bo.TpWechatBo;
import org.dromara.work.domain.vo.TpWechatVo;
import org.dromara.work.domain.vo.WechatSumVo;
import org.dromara.work.service.ITpWechatService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 微信管理
 *
 * @author Maosw
 * @date 2024-08-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/work/wechat")
public class TpWechatController extends BaseController {

    private final ITpWechatService tpWechatService;

    /**
     * 查询微信管理列表
     */
    @SaCheckPermission("work:wechat:list")
    @GetMapping("/list")
    public TableDataInfo<TpWechatVo> list(TpWechatBo bo, PageQuery pageQuery) {
        return tpWechatService.queryPageList(bo, pageQuery);
    }


    /**
     * 查询微信统计
     */
    @SaCheckPermission("work:wechat:sum")
    @GetMapping("/wechatSum")
    public R<WechatSumVo> wechatSum(TpWechatBo bo) {
        /*LoginUser loginUser = LoginHelper.getLoginUser();
        if(!LoginHelper.isSuperAdmin()){
            if(loginUser.getIdentity() == 2){
                throw new ServiceException("您无权限查看微信信息");
            } else if (loginUser.getIdentity() == 3) {
                bo.setUid(loginUser.getUserId());
            }
        }*/
        return R.ok(tpWechatService.queryListSum(bo));
    }


    /**
     * 导出客户微信
     */
    @SaCheckPermission("work:wechat:export")
    @Log(title = "导出客户微信", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TpWechatBo bo, HttpServletResponse response) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if(!LoginHelper.isSuperAdmin()){
            bo.setUid(loginUser.getUserId());
        }
        List<TpWechatVo> list = tpWechatService.queryList(bo);
        ExcelUtil.exportExcel(list, "客户微信导出", TpWechatVo.class, response);
    }

    /**
     * 获取微信管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("work:wechat:query")
    @GetMapping("/{id}")
    public R<TpWechatVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tpWechatService.queryById(id));
    }

    /**
     * 新增微信管理
     */
    @SaCheckPermission("work:wechat:add")
    @Log(title = "微信管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TpWechatBo bo) {
        return toAjax(tpWechatService.insertByBo(bo));
    }

    /**
     * 修改微信管理
     */
    @SaCheckPermission("work:wechat:edit")
    @Log(title = "微信管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TpWechatBo bo) {
        return toAjax(tpWechatService.updateByBo(bo));
    }


    /**
     * 修改使用人
     */
    @SaCheckPermission("work:wechat:editUser")
    @Log(title = "修改使用人", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/editUser")
    public R<Void> editUser(@Validated(EditGroup.class) @RequestBody TpWechatBo bo) {
        return toAjax(tpWechatService.updateUserByBo(bo));
    }


    /**
     * 删除微信管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("work:wechat:remove")
    @Log(title = "微信管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(tpWechatService.deleteWithValidByIds(List.of(ids), true));
    }
}
