package com.propertyManagement.web.controller.business;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.propertyManagement.business.domain.WxLiveBill;
import com.propertyManagement.business.domain.bo.WxLiveBillBo;
import com.propertyManagement.business.domain.vo.WxLiveBillVo;
import com.propertyManagement.business.mapper.WxLiveBillMapper;
import com.propertyManagement.business.service.IWxLiveBillService;
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.helper.LoginHelper;
import com.propertyManagement.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * 生活缴费账单
 *
 * @author liushuai
 * @date 2025-05-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/liveBill")
public class WxLiveBillController extends BaseController {

    private final IWxLiveBillService iWxLiveBillService;

    private final WxLiveBillMapper wxLiveBillMapper;

    /**
     * 查询生活缴费账单列表
     */
    @SaCheckPermission("business:liveBill:list")
    @GetMapping("/list")
    public TableDataInfo<WxLiveBillVo> list(WxLiveBillBo bo, PageQuery pageQuery) {
        if (!LoginHelper.isAdmin()) {
            bo.setCommunityId(LoginHelper.getLoginUser().getCommunityId());
        }
        return iWxLiveBillService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出生活缴费账单列表
     */
    @SaCheckPermission("business:liveBill:export")
    @Log(title = "生活缴费账单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WxLiveBillBo bo, HttpServletResponse response) {
        if (!LoginHelper.isAdmin()) {
            bo.setCommunityId(LoginHelper.getLoginUser().getCommunityId());
        }
        List<WxLiveBillVo> list = iWxLiveBillService.queryList(bo);
        ExcelUtil.exportExcel(list, "生活缴费账单", WxLiveBillVo.class, response);
    }

    /**
     * 获取生活缴费账单详细信息
     *
     * @param billId 主键
     */
    @SaCheckPermission("business:liveBill:query")
    @GetMapping("/{billId}")
    public R<WxLiveBillVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long billId) {
        return R.ok(iWxLiveBillService.queryById(billId));
    }

    /**
     * 新增生活缴费账单
     */
    @SaCheckPermission("business:liveBill:add")
    @Log(title = "生活缴费账单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WxLiveBillBo bo) {
        return toAjax(iWxLiveBillService.insertByBo(bo));
    }

    /**
     * 修改生活缴费账单
     */
    @SaCheckPermission("business:liveBill:edit")
    @Log(title = "生活缴费账单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WxLiveBillBo bo) {
        return toAjax(iWxLiveBillService.updateByBo(bo));
    }

    /**
     * 删除生活缴费账单
     *
     * @param billIds 主键串
     */
    @SaCheckPermission("business:liveBill:remove")
    @Log(title = "生活缴费账单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{billIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] billIds) {
        return toAjax(iWxLiveBillService.deleteWithValidByIds(Arrays.asList(billIds), true));
    }

    /**
     * 确认缴费账单
     *
     * @param billIds 主键串
     */
    @Log(title = "确认缴费账单", businessType = BusinessType.UPDATE)
    @PutMapping("/confirm/{billIds}")
    public R<Void> confirm(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] billIds) {
        LambdaUpdateWrapper<WxLiveBill> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.in(WxLiveBill::getBillId, billIds);
        updateWrapper.set(WxLiveBill::getState, 2);
        wxLiveBillMapper.update(updateWrapper);
        return toAjax( wxLiveBillMapper.update(updateWrapper));
    }
}
