package cn.iocoder.yudao.module.system.controller.app.devuce;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.dishes.vo.DishesRespVO;
import cn.iocoder.yudao.module.system.controller.admin.dishestype.vo.DishesTypeRespVO;
import cn.iocoder.yudao.module.system.controller.app.devuce.vo.DevuceReqVO;
import cn.iocoder.yudao.module.system.controller.app.devuce.vo.DevuceRespVO;
import cn.iocoder.yudao.module.system.controller.app.devuce.vo.DevuceSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dishes.DishesDO;
import cn.iocoder.yudao.module.system.dal.dataobject.dishestype.DishesTypeDO;
import cn.iocoder.yudao.module.system.service.deviceinfo.DeviceInfoService;
import cn.iocoder.yudao.module.system.service.devuce.DevuceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;



@Tag(name = "管理后台 - 设备")
@RestController
@RequestMapping("/t/devuce")
@Validated
@PermitAll
public class AppDevuceController {

    @Resource
    private DevuceService devuceService;
    @Resource
    private DeviceInfoService deviceInfoService;

//    @PostMapping("/create")
//    @Operation(summary = "创建设备")
//    public CommonResult<Long> createDevuce(@Valid @RequestBody DevuceSaveReqVO createReqVO) {
//        return success(devuceService.createDevuce(createReqVO));
//    }
    /**
     * @Description: 进行解绑与绑定操作
     * @Author: qjq
     * @Date: 2024/4/7 15:23
     * @return
     */
    @PutMapping("/update")
    @Operation(summary = "更新设备")
    public CommonResult<Boolean> updateDevuce(@Valid @RequestBody DevuceSaveReqVO updateReqVO) {
        devuceService.updateDevuce(updateReqVO);
        return success(true);
    }

//    @DeleteMapping("/delete")
//    @Operation(summary = "删除设备")
//    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('t:devuce:delete')")
//    public CommonResult<Boolean> deleteDevuce(@RequestParam("id") Long id) {
//        devuceService.deleteDevuce(id);
//        return success(true);
//    }

//    @GetMapping("/get")
//    @Operation(summary = "获得设备")
//    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    public CommonResult<DevuceRespVO> getDevuce(@RequestParam("id") Long id) {
//        DevuceDO devuce = devuceService.getDevuce(id);
//        return success(BeanUtils.toBean(devuce, DevuceRespVO.class));
//    }
    @GetMapping("/dishes/list")
    @Operation(summary = "获得菜品列表")
    public  CommonResult<List<DishesRespVO>> getDishesList(Long carteenId,Long dishecType,String dishesName) {
        List<DishesDO> dishesList = devuceService.getDishesList(carteenId,dishecType,dishesName);
        return success(BeanUtils.toBean(dishesList, DishesRespVO.class));
    }

    @GetMapping("/dishes/page")
    @Operation(summary = "获得菜品列表分页")
    public  CommonResult< PageResult<DishesRespVO>> getDishesPage(Long carteenId,Long dishecType,String dishesName,Integer pageNo,Integer pageSize) {
        PageResult<DishesDO> dishesList = devuceService.getDishesPage(carteenId,dishecType,dishesName,pageNo,pageSize);
        return success(BeanUtils.toBean(dishesList, DishesRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得绑定的菜品")
    public CommonResult<DishesRespVO> getDevuceList(){
        DishesDO devuceList = devuceService.getDevuceList();
        return success(BeanUtils.toBean(devuceList, DishesRespVO.class));
    }
    @GetMapping("/type")
    @Operation(summary = "获取菜品分类")
    public CommonResult<List<DishesTypeRespVO>> getDishesTypeList(@RequestParam("carteenId") Long carteenId) {
        List<DishesTypeDO> dishesTypeList = devuceService.getDishesTypeList(carteenId);
        return success(BeanUtils.toBean(dishesTypeList, DishesTypeRespVO.class));
    }
//    @GetMapping("/page")
//    @Operation(summary = "获得设备分页")
//    public CommonResult<PageResult<DevuceRespVO>> getDevucePage(@Valid DevucePageReqVO pageReqVO) {
//        PageResult<DevuceDO> pageResult = devuceService.getDevucePage(pageReqVO);
//        return success(BeanUtils.toBean(pageResult, DevuceRespVO.class));
//    }

//    @GetMapping("/export-excel")
//    @Operation(summary = "导出设备 Excel")
//    @PreAuthorize("@ss.hasPermission('t:devuce:export')")
//    @OperateLog(type = EXPORT)
//    public void exportDevuceExcel(@Valid DevucePageReqVO pageReqVO,
//              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<DevuceDO> list = devuceService.getDevucePage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "设备.xls", "数据", DevuceRespVO.class,
//                        BeanUtils.toBean(list, DevuceRespVO.class));
//    }
    @GetMapping("/carteen")
    @Operation(summary = "根据ip获取门店id")
    public CommonResult<Long> getCarteenId() {
        return success(devuceService.getCarteenIdByIp());
    }

    @GetMapping("/carteenNew")
    @Operation(summary = "根据ip获取门店id")
    public CommonResult<Map<String,Object>> getCarteenIdNew() {
        return success(devuceService.getCarteenIdByIpNew());
    }


    @GetMapping("/nutrition")
    @Operation(summary = "获取菜品营养成分")
    public CommonResult<List<Map>> getDishesNutrition(@RequestParam("dishesId") Long dishesId,@RequestParam(value = "cId",required = false) String cId) {
        return success(devuceService.getDishesNutrition( dishesId,cId));
    }

    @PostMapping("/updateWeight")
    @Operation(summary = "更改菜品剩余重量")
    public CommonResult<Boolean> updateWeight( @RequestBody DevuceReqVO updateReqVO) {
        devuceService.updateWeight(updateReqVO);
        return success(true);
    }

    @PutMapping("/addWeight")
    @Operation(summary = "增加菜品总量")
    public CommonResult<Boolean> addWeight(BigDecimal addWeight) {
        devuceService.addWeight(addWeight);
        return success(true);
    }

    @GetMapping("/alive")
    @Operation(summary = "在线检测")
    public void alive() {
        deviceInfoService.alive();
    }


}