package com.propertyManagement.web.controller.business;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.propertyManagement.business.domain.WxComment;
import com.propertyManagement.business.domain.bo.WxCommentBo;
import com.propertyManagement.business.domain.vo.WxCommentVo;
import com.propertyManagement.business.mapper.WxCommentMapper;
import com.propertyManagement.business.service.IWxCommentService;
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.enums.RepairsState;
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.Date;
import java.util.List;

/**
 * 表扬与投诉
 *
 * @author liushuai
 * @date 2025-05-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/comment")
public class WxCommentController extends BaseController {

    private final IWxCommentService iWxCommentService;
    private final WxCommentMapper commentMapper;

    /**
     * 查询表扬与投诉列表
     */
    @SaCheckPermission("business:comment:list")
    @GetMapping("/list")
    public TableDataInfo<WxCommentVo> list(WxCommentBo bo, PageQuery pageQuery) {
        if (!LoginHelper.isAdmin()) {
            bo.setCommunityId(LoginHelper.getLoginUser().getCommunityId());
        }
        return iWxCommentService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出表扬与投诉列表
     */
    @SaCheckPermission("business:comment:export")
    @Log(title = "表扬与投诉", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WxCommentBo bo, HttpServletResponse response) {
        if (!LoginHelper.isAdmin()) {
            bo.setCommunityId(LoginHelper.getLoginUser().getCommunityId());
        }
        List<WxCommentVo> list = iWxCommentService.queryList(bo);
        ExcelUtil.exportExcel(list, "表扬与投诉", WxCommentVo.class, response);
    }

    /**
     * 获取表扬与投诉详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("business:comment:query")
    @GetMapping("/{id}")
    public R<WxCommentVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iWxCommentService.queryById(id));
    }

    /**
     * 新增表扬与投诉
     */
    @SaCheckPermission("business:comment:add")
    @Log(title = "表扬与投诉", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WxCommentBo bo) {
        return toAjax(iWxCommentService.insertByBo(bo));
    }

    /**
     * 修改表扬与投诉
     */
    @SaCheckPermission("business:comment:edit")
    @Log(title = "表扬与投诉", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WxCommentBo bo) {
        return toAjax(iWxCommentService.updateByBo(bo));
    }

    /**
     * 删除表扬与投诉
     *
     * @param ids 主键串
     */
    @SaCheckPermission("business:comment:remove")
    @Log(title = "表扬与投诉", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iWxCommentService.deleteWithValidByIds(Arrays.asList(ids), true));
    }


    /**
     * 取消表扬或投诉
     *
     * @param id 主键
     */
    @PutMapping("/complete/{id}")
    public R<Void> completeComment(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        WxComment wxComment = commentMapper.selectById(id);
        if (wxComment == null) {
            return R.fail("操作失败，工单不存在");
        }
        if (!wxComment.getState().equals(RepairsState.ONE.getCode())) {
            return R.fail("操作失败，工单状态错误");
        }
        wxComment.setState(2);
        wxComment.setCompleteTime(new Date());
        commentMapper.updateById(wxComment);
        return toAjax(true);
    }
}
