package com.ruoyi.web.controller.app;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.ServiceConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.ValidGroup;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.ss.equipment.domain.Equipment;
import com.ruoyi.ss.equipment.domain.EquipmentQuery;
import com.ruoyi.ss.equipment.domain.EquipmentVO;
import com.ruoyi.ss.equipment.service.IEquipmentAssembler;
import com.ruoyi.ss.equipment.service.IEquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author qzz
 * 2024/7/22
 */
@RestController
@RequestMapping("/app/equipment")
public class AppEquipmentController extends BaseController {

    @Autowired
    private IEquipmentService equipmentService;

    @Autowired
    private IEquipmentAssembler equipmentAssembler;

    /**
     * 查询设施列表
     */
    @GetMapping("/list")
    public TableDataInfo list(EquipmentQuery query)
    {
        startPage();
        startOrderBy();
        List<EquipmentVO> list = equipmentService.selectEquipmentList(query);
        equipmentAssembler.assembleDeviceInfo(list);
//        equipmentAssembler.assembleRuleInfo(list);
        return getDataTable(list);
    }

    /**
     * 获取设施详细信息
     */
    @GetMapping(value = "/{equipmentId}")
    public AjaxResult getInfo(@PathVariable("equipmentId") Long equipmentId)
    {
        EquipmentVO equipmentVO = equipmentService.selectEquipmentByEquipmentId(equipmentId);
        equipmentAssembler.assembleDeviceInfo(equipmentVO);
//        equipmentAssembler.assembleRuleInfo(equipmentVO);
        return success(equipmentVO);
    }

    /**
     * 新增设施
     */
    @Log(title = "设施", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated(ValidGroup.Create.class) Equipment equipment)
    {
        logger.info("【新增设施】，请求参数=【{}】", JSON.toJSON(equipment));
//        if(ServiceConstants.FACILITY_TYPE_TOILET_DOOR.equals(equipment.getType())){
//            ServiceUtil.assertion(equipment.getUnlockMode() == null, "卫生间门开锁方式不能为空");
//            ServiceUtil.assertion(equipment.getUnlockCondition() == null, "卫生间门开锁条件不能为空");
//            ServiceUtil.assertion(equipment.getUnlockTime() == null, "卫生间门开锁时长不能为空");
//        }
        return toAjax(equipmentService.insertEquipment(equipment));
    }

    /**
     * 修改设施
     */
    @Log(title = "设施", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated(ValidGroup.Update.class) Equipment equipment)
    {
        logger.info("【修改设施】，请求参数=【{}】", JSON.toJSON(equipment));
        return toAjax(equipmentService.updateEquipment(equipment));
    }

    /**
     * 删除设施
     */
    @Log(title = "设施", businessType = BusinessType.DELETE)
    @DeleteMapping("/{equipmentIds}")
    public AjaxResult remove(@PathVariable Long[] equipmentIds)
    {
        logger.info("删除设施：【{}】", JSON.toJSONString(equipmentIds));
        return toAjax(equipmentService.deleteEquipmentByEquipmentIds(equipmentIds));
    }


//    /**
//     * 根据店铺查大门
//     */
//    @Log(title = "根据店铺查大门", businessType = BusinessType.UPDATE)
//    @GetMapping("/gateGate")
//    public AjaxResult gateGate(Long storeId)
//    {
//        ServiceUtil.assertion(storeId == null, "店铺ID不能为空");
//        logger.info("根据店铺查大门：【{}】",storeId);
////        EquipmentVO vo = equipmentService.selectGate(storeId);
//        equipmentAssembler.assembleDeviceInfo(vo);
//        return success(vo);
//    }
}
