package com.propertyManagement.web.controller.business;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.propertyManagement.business.domain.bo.WxProprietorLedgerBo;
import com.propertyManagement.business.domain.vo.WxProprietorLedgerVo;
import com.propertyManagement.business.listener.WxProprietorLedgerImportListener;
import com.propertyManagement.business.service.IWxProprietorLedgerService;
import com.propertyManagement.common.annotation.Log;
import com.propertyManagement.common.annotation.RepeatSubmit;
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.page.TableDataInfo;
import com.propertyManagement.common.core.validate.AddGroup;
import com.propertyManagement.common.core.validate.EditGroup;
import com.propertyManagement.common.enums.BusinessType;
import com.propertyManagement.common.excel.ExcelResult;
import com.propertyManagement.common.helper.LoginHelper;
import com.propertyManagement.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 小区业主台账信息
 *
 * @author liushuai
 * @date 2025-05-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/proprietorLedger")
public class WxProprietorLedgerController extends BaseController {

    private final IWxProprietorLedgerService iWxProprietorLedgerService;

    /**
     * 查询小区业主台账信息列表
     */
    @SaCheckPermission("business:proprietorLedger:list")
    @GetMapping("/list")
    public TableDataInfo<WxProprietorLedgerVo> list(WxProprietorLedgerBo bo, PageQuery pageQuery) {
        if (!LoginHelper.isAdmin()) {
            bo.setCommunityId(LoginHelper.getLoginUser().getCommunityId());
        }
        return iWxProprietorLedgerService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询小区业主台账信息列表
     */
    @SaCheckPermission("business:proprietorLedger:list")
    @GetMapping("/listAll")
    public R<List<WxProprietorLedgerVo>> listAll(WxProprietorLedgerBo bo) {
        if (!LoginHelper.isAdmin()) {
            bo.setCommunityId(LoginHelper.getLoginUser().getCommunityId());
        }
        return R.ok(iWxProprietorLedgerService.queryList(bo));
    }

    /**
     * 导出小区业主台账信息列表
     */
    @SaCheckPermission("business:proprietorLedger:export")
    @Log(title = "小区业主台账信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WxProprietorLedgerBo bo, HttpServletResponse response) {
        if (!LoginHelper.isAdmin()) {
            bo.setCommunityId(LoginHelper.getLoginUser().getCommunityId());
        }
        List<WxProprietorLedgerVo> list = iWxProprietorLedgerService.queryList(bo);
        ExcelUtil.exportExcel(list, "小区业主台账信息", WxProprietorLedgerVo.class, response);
    }

    /**
     * 业主台账导入模板下载
     */
    @PostMapping("/importTemplate")
    public void importTemplate(WxProprietorLedgerBo bo, HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "业主台账模板", WxProprietorLedgerVo.class, response);
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "业主台账导入", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, Long communityId, boolean updateSupport) throws Exception {
        ExcelResult<WxProprietorLedgerVo> result = ExcelUtil.importExcel(file.getInputStream(), WxProprietorLedgerVo.class, new WxProprietorLedgerImportListener(communityId, updateSupport));
        return R.ok(result.getAnalysis());
    }

    /**
     * 获取小区业主台账信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("business:proprietorLedger:query")
    @GetMapping("/{id}")
    public R<WxProprietorLedgerVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iWxProprietorLedgerService.queryById(id));
    }

    /**
     * 新增小区业主台账信息
     */
    @SaCheckPermission("business:proprietorLedger:add")
    @Log(title = "小区业主台账信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WxProprietorLedgerBo bo) {
        return toAjax(iWxProprietorLedgerService.insertByBo(bo));
    }

    /**
     * 修改小区业主台账信息
     */
    @SaCheckPermission("business:proprietorLedger:edit")
    @Log(title = "小区业主台账信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WxProprietorLedgerBo bo) {
        return toAjax(iWxProprietorLedgerService.updateByBo(bo));
    }

    /**
     * 删除小区业主台账信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("business:proprietorLedger:remove")
    @Log(title = "小区业主台账信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iWxProprietorLedgerService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
