package com.xjrsoft.module.prdManage.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
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 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.datalog.service.DatalogService;
import com.xjrsoft.module.prdManage.dto.AddTestfrom2Dto;
import com.xjrsoft.module.prdManage.dto.Testfrom2PageDto;
import com.xjrsoft.module.prdManage.dto.UpdateTestfrom2Dto;
import com.xjrsoft.module.prdManage.entity.Testfrom2;
import com.xjrsoft.module.prdManage.service.ITestfrom2Service;
import com.xjrsoft.module.prdManage.vo.Testfrom2PageVo;
import com.xjrsoft.module.prdManage.vo.Testfrom2Vo;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;

/**
* @title: 测试
* @Author 管理员
* @Date: 2025-08-20
* @Version 1.0
*/
@RestController
@RequestMapping("/dev" + "/testfrom2")
@Api(value = "/dev"  + "/testfrom2",tags = "测试代码")
@AllArgsConstructor
public class Testfrom2Controller {


    private final ITestfrom2Service testfrom2Service;
    
    private final DatalogService dataService;


    @GetMapping(value = "/page")
    @ApiOperation(value="Testfrom2列表(分页)")
    @SaCheckPermission("testfrom2:list")
    public R page(@Valid Testfrom2PageDto dto){

        LambdaQueryWrapper<Testfrom2> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .like(StrUtil.isNotBlank(dto.getDanXingWenBen7818()),Testfrom2::getDanXingWenBen7818,dto.getDanXingWenBen7818())
                .like(StrUtil.isNotBlank(dto.getDanXingWenBen9043()),Testfrom2::getDanXingWenBen9043,dto.getDanXingWenBen9043())
                    .orderByDesc(Testfrom2::getId)
                .select(Testfrom2.class,x -> VoToColumnUtil.fieldsToColumns(Testfrom2PageVo.class).contains(x.getProperty()));
        IPage<Testfrom2> page = testfrom2Service.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<Testfrom2PageVo> pageOutput = ConventPage.getPageOutput(page, Testfrom2PageVo.class);
        return R.ok(pageOutput);
    }

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


    @PostMapping
    @ApiOperation(value = "新增Testfrom2")
    @SaCheckPermission("testfrom2:add")
    public R add(@Valid @RequestBody AddTestfrom2Dto dto){
       // Testfrom2 testfrom2 = BeanUtil.toBean(dto, Testfrom2.class);
        //boolean isSuccess = testfrom2Service.save(testfrom2);
		AddTestfrom2Dto res = dataService.insert(dto);
        return R.ok(res.getId());
    }

    @PutMapping
    @ApiOperation(value = "修改Testfrom2")
    @SaCheckPermission("testfrom2:edit")
    public R update(@Valid @RequestBody AddTestfrom2Dto dto){

       // Testfrom2 testfrom2 = BeanUtil.toBean(dto, Testfrom2.class);
        return R.ok(dataService.updateById(dto));

    }

    @DeleteMapping
    @ApiOperation(value = "删除")
    @SaCheckPermission("testfrom2:delete")
    public R delete(@Valid @RequestBody List<Long> ids){
        //return R.ok(testfrom2Service.removeBatchByIds(ids));
        return R.ok(dataService.deleteByIds(AddTestfrom2Dto.class, ids));
    }

}