package com.xjrsoft.module.prdManage.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.model.result.R;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.prdManage.dto.AddDemoApplianceDto;
import com.xjrsoft.module.prdManage.dto.DemoAppliancePageDto;
import com.xjrsoft.module.prdManage.dto.UpdateDemoApplianceDto;
import com.xjrsoft.module.prdManage.entity.DemoAppliance;
import com.xjrsoft.module.prdManage.service.IDemoApplianceService;
import com.xjrsoft.module.prdManage.vo.DemoAppliancePageVo;
import com.xjrsoft.module.prdManage.vo.DemoApplianceVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
* @title: 示例-家电管理
* @Author 管理员
* @Date: 2025-03-12
* @Version 1.0
*/
@RestController
@RequestMapping("/prdManage" + "/demoAppliance")
@Api(value = "/prdManage"  + "/demoAppliance",tags = "示例-家电管理代码")
@AllArgsConstructor
public class DemoApplianceController {


private final IDemoApplianceService demoApplianceService;

    @GetMapping(value = "/page")
    @ApiOperation(value="DemoAppliance列表(分页)")
    @SaCheckPermission("demoAppliance:list")
    public R page(@Valid DemoAppliancePageDto dto){

    LambdaQueryWrapper<DemoAppliance> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper
            .like(StrUtil.isNotBlank(dto.getName()),DemoAppliance::getName,dto.getName())
            .like(StrUtil.isNotBlank(dto.getSerialNumber()),DemoAppliance::getSerialNumber,dto.getSerialNumber())
            .like(StrUtil.isNotBlank(dto.getCategory()),DemoAppliance::getCategory,dto.getCategory())
            .orderByDesc(DemoAppliance::getId)
    .select(DemoAppliance.class,x -> VoToColumnUtil.fieldsToColumns(DemoAppliancePageVo.class).contains(x.getProperty()));
    IPage<DemoAppliance> page = demoApplianceService.page(ConventPage.getPage(dto), queryWrapper);
    PageOutput<DemoAppliancePageVo> pageOutput = ConventPage.getPageOutput(page, DemoAppliancePageVo.class);
        return R.ok(pageOutput);
        }

        @GetMapping(value = "/info")
        @ApiOperation(value="根据id查询DemoAppliance信息")
        @SaCheckPermission("demoAppliance:detail")
        public R info(@RequestParam Integer id){
        DemoAppliance demoAppliance = demoApplianceService.getById(id);
        if (demoAppliance == null) {
        return R.error("找不到此数据！");
        }
        return R.ok(BeanUtil.toBean(demoAppliance, DemoApplianceVo.class));
        }


        @PostMapping
        @ApiOperation(value = "新增DemoAppliance")
        @SaCheckPermission("demoAppliance:add")
        public R add(@Valid @RequestBody AddDemoApplianceDto dto){
            DemoAppliance demoAppliance = BeanUtil.toBean(dto, DemoAppliance.class);
            boolean isSuccess = demoApplianceService.save(demoAppliance);
        return R.ok(demoAppliance.getId());
        }

        @PutMapping
        @ApiOperation(value = "修改DemoAppliance")
        @SaCheckPermission("demoAppliance:edit")
        public R update(@Valid @RequestBody UpdateDemoApplianceDto dto){

            DemoAppliance demoAppliance = BeanUtil.toBean(dto, DemoAppliance.class);
            return R.ok(demoApplianceService.updateById(demoAppliance));

        }

        @DeleteMapping
        @ApiOperation(value = "删除")
        @SaCheckPermission("demoAppliance:delete")
        public R delete(@Valid @RequestBody List<Integer> ids){
            return R.ok(demoApplianceService.removeBatchByIds(ids));

        }

                            }