package com.propertyManagement.web.controller.business.api;

import cn.hutool.core.bean.BeanUtil;
import com.propertyManagement.business.domain.bo.WxRepairsBo;
import com.propertyManagement.business.domain.vo.LoginWxUser;
import com.propertyManagement.business.domain.vo.WxRepairsVo;
import com.propertyManagement.business.service.IWxRepairsService;
import com.propertyManagement.business.support.util.AuthUtil;
import com.propertyManagement.common.annotation.RepeatSubmit;
import com.propertyManagement.common.core.controller.BaseController;
import com.propertyManagement.common.core.domain.R;
import com.propertyManagement.common.core.validate.AddGroup;
import com.propertyManagement.common.enums.RepairsState;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 报修
 *
 * @author liushuai
 * @date 2025-05-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/repairs")
public class ApiWxRepairsController extends BaseController {

    private final IWxRepairsService iWxRepairsService;

    /**
     * 查询用户报修列表
     */
    @GetMapping("/userListAll")
    public R<List<WxRepairsVo>> userListAll(WxRepairsBo bo) {
        LoginWxUser wxUser = AuthUtil.getWxUser();
        bo.setCommunityId(wxUser.getCommunityId());
        bo.setUserId(wxUser.getId());
        return R.ok(iWxRepairsService.queryList(bo));
    }

    /**
     * 查询维修报修列表
     */
    @GetMapping("/maintainListAll")
    public R<List<WxRepairsVo>> maintainListAll(WxRepairsBo bo) {
        LoginWxUser wxUser = AuthUtil.getWxUser();
        bo.setCommunityId(wxUser.getCommunityId());
        bo.setAccountId(wxUser.getAccountId());
        return R.ok(iWxRepairsService.queryList(bo));
    }

    /**
     * 获取报修单详细信息
     *
     * @param repairsId 主键
     */
    @GetMapping("/{repairsId}")
    public R<WxRepairsVo> getInfo(@NotNull(message = "报修单ID不能为空")
                                     @PathVariable Long repairsId) {
        return R.ok(iWxRepairsService.queryById(repairsId));
    }

    /**
     * 新增报修
     */
    @RepeatSubmit()
    @PostMapping("/add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WxRepairsBo bo) {
        return toAjax(iWxRepairsService.add(bo));
    }

    /**
     * 取消报修
     *
     * @param repairsId 主键
     */
    @PutMapping("/cancel/{repairsId}")
    public R<Void> cancel(@NotNull(message = "主键不能为空")
                          @PathVariable Long repairsId) {
        WxRepairsVo repairsVo = iWxRepairsService.queryById(repairsId);
        if (null == repairsVo) {
            return R.fail("报修单不存在，请刷新后重试！");
        }
        if (!repairsVo.getState().equals(RepairsState.ONE.getCode())) {
            return R.fail("操作失败，报修单状态错误！");
        }
        WxRepairsBo repairsBo = BeanUtil.toBean(repairsVo, WxRepairsBo.class);
        repairsBo.setState(RepairsState.FOUR.getCode());
        repairsBo.setCloseTime(new Date());
        return toAjax(iWxRepairsService.updateByBo(repairsBo));
    }

    /**
     * 设置维修费用
     *
     * @param repairsId 主键
     */
    @PutMapping("/setMoney/{repairsId}/{money}")
    public R<Void> setMoney(@NotNull(message = "主键不能为空")
                            @PathVariable Long repairsId,
                            @NotNull(message = "金额不能为空")
                            @PathVariable BigDecimal money) {
        WxRepairsVo repairsVo = iWxRepairsService.queryById(repairsId);
        if (null == repairsVo) {
            return R.fail("报修单不存在，请刷新后重试！");
        }
        if (!repairsVo.getState().equals(RepairsState.ONE.getCode())) {
            return R.fail("操作失败，报修单状态错误！");
        }
        WxRepairsBo repairsBo = BeanUtil.toBean(repairsVo, WxRepairsBo.class);
        repairsBo.setState(RepairsState.TWO.getCode());
        return toAjax(iWxRepairsService.updateByBo(repairsBo));
    }

    /**
     * 支付报修单
     *
     * @param repairsId 主键
     */
    @PutMapping("/pay/{repairsId}")
    public R<Void> pay(@NotEmpty(message = "主键不能为空") @PathVariable Long repairsId) {

        return toAjax(true);
    }
}
