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;
import java.util.Map;

/**
 * 报修
 *
 * @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("/userCountAll")
    public R<Map<Integer, Long>> userCountAll() {
        return R.ok(iWxRepairsService.countAll());
    }

    /**
     * 查询维修人员报修状态统计
     */
    @GetMapping("/maintainCountAll")
    public R<Map<Integer, Long>> maintainCountAll() {
        return R.ok(iWxRepairsService.maintainCountAll());
    }

    /**
     * 查询维修报修列表
     */
    @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());
        repairsBo.setMoney(money);
        return toAjax(iWxRepairsService.updateByBo(repairsBo));
    }
}
