package com.propertyManagement.web.controller.business;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.propertyManagement.business.domain.bo.WxCommonPhoneBo;
import com.propertyManagement.business.domain.vo.WxCommonPhoneVo;
import com.propertyManagement.business.service.IWxCommonPhoneService;
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.domain.model.LoginUser;
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.util.function.SupplierUtils;
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/commonPhone")
public class WxCommonPhoneController extends BaseController {

    private final IWxCommonPhoneService iWxCommonPhoneService;

    /**
     * 查询常用电话列表
     */
    @SaCheckPermission("business:commonPhone:list")
    @GetMapping("/list")
    public TableDataInfo<WxCommonPhoneVo> list(WxCommonPhoneBo bo, PageQuery pageQuery) {
        if (!LoginHelper.isAdmin()) {
            bo.setCommunityId(LoginHelper.getLoginUser().getCommunityId());
        }
        return iWxCommonPhoneService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出常用电话列表
     */
    @SaCheckPermission("business:commonPhone:export")
    @Log(title = "常用电话", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WxCommonPhoneBo bo, HttpServletResponse response) {
        if (!LoginHelper.isAdmin()) {
            bo.setCommunityId(LoginHelper.getLoginUser().getCommunityId());
        }
        List<WxCommonPhoneVo> list = iWxCommonPhoneService.queryList(bo);
        ExcelUtil.exportExcel(list, "常用电话", WxCommonPhoneVo.class, response);
    }

    /**
     * 获取常用电话详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("business:commonPhone:query")
    @GetMapping("/{id}")
    public R<WxCommonPhoneVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iWxCommonPhoneService.queryById(id));
    }

    /**
     * 新增常用电话
     */
    @SaCheckPermission("business:commonPhone:add")
    @Log(title = "常用电话", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WxCommonPhoneBo bo) {
        return toAjax(iWxCommonPhoneService.insertByBo(bo));
    }

    /**
     * 修改常用电话
     */
    @SaCheckPermission("business:commonPhone:edit")
    @Log(title = "常用电话", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WxCommonPhoneBo bo) {
        return toAjax(iWxCommonPhoneService.updateByBo(bo));
    }

    /**
     * 删除常用电话
     *
     * @param ids 主键串
     */
    @SaCheckPermission("business:commonPhone:remove")
    @Log(title = "常用电话", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iWxCommonPhoneService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
