package com.propertyManagement.web.controller.business;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.propertyManagement.common.helper.LoginHelper;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.propertyManagement.common.annotation.RepeatSubmit;
import com.propertyManagement.common.annotation.Log;
import com.propertyManagement.common.core.controller.BaseController;
import com.propertyManagement.common.core.domain.PageQuery;
import com.propertyManagement.common.core.domain.R;
import com.propertyManagement.common.core.validate.AddGroup;
import com.propertyManagement.common.core.validate.EditGroup;
import com.propertyManagement.common.core.validate.QueryGroup;
import com.propertyManagement.common.enums.BusinessType;
import com.propertyManagement.common.utils.poi.ExcelUtil;
import com.propertyManagement.business.domain.vo.WxAccountVo;
import com.propertyManagement.business.domain.bo.WxAccountBo;
import com.propertyManagement.business.service.IWxAccountService;
import com.propertyManagement.common.core.page.TableDataInfo;

/**
 * 员工账号
 *
 * @author liushuai
 * @date 2025-05-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/account")
public class WxAccountController extends BaseController {

    private final IWxAccountService iWxAccountService;

    /**
     * 查询员工账号列表
     */
    @SaCheckPermission("business:account:list")
    @GetMapping("/list")
    public TableDataInfo<WxAccountVo> list(WxAccountBo bo, PageQuery pageQuery) {
        if (!LoginHelper.isAdmin()) {
            bo.setCommunityId(LoginHelper.getLoginUser().getCommunityId());
        }
        return iWxAccountService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询员工账号列表
     */
    @SaCheckPermission("business:account:list")
    @GetMapping("/listAll")
    public R<List<WxAccountVo>> listAll(WxAccountBo bo) {
        return R.ok(iWxAccountService.queryList(bo));
    }

    /**
     * 导出员工账号列表
     */
    @SaCheckPermission("business:account:export")
    @Log(title = "员工账号", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WxAccountBo bo, HttpServletResponse response) {
        if (!LoginHelper.isAdmin()) {
            bo.setCommunityId(LoginHelper.getLoginUser().getCommunityId());
        }
        List<WxAccountVo> list = iWxAccountService.queryList(bo);
        ExcelUtil.exportExcel(list, "员工账号", WxAccountVo.class, response);
    }

    /**
     * 获取员工账号详细信息
     *
     * @param accountId 主键
     */
    @SaCheckPermission("business:account:query")
    @GetMapping("/{accountId}")
    public R<WxAccountVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long accountId) {
        return R.ok(iWxAccountService.queryById(accountId));
    }

    /**
     * 新增员工账号
     */
    @SaCheckPermission("business:account:add")
    @Log(title = "员工账号", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WxAccountBo bo) {
        return toAjax(iWxAccountService.insertByBo(bo));
    }

    /**
     * 修改员工账号
     */
    @SaCheckPermission("business:account:edit")
    @Log(title = "员工账号", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WxAccountBo bo) {
        return toAjax(iWxAccountService.updateByBo(bo));
    }

    /**
     * 删除员工账号
     *
     * @param accountIds 主键串
     */
    @SaCheckPermission("business:account:remove")
    @Log(title = "员工账号", businessType = BusinessType.DELETE)
    @DeleteMapping("/{accountIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] accountIds) {
        return toAjax(iWxAccountService.deleteWithValidByIds(Arrays.asList(accountIds), true));
    }
}
