package com.sqx.modules.artificerselfgoods.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.common.utils.Result;
import com.sqx.modules.artificerselfgoods.entity.BlArtificerSelfGoods;
import com.sqx.modules.artificerselfgoods.service.BlArtificerSelfGoodsService;
import com.sqx.modules.artificerselfgoods.service.BlArtificerSelfGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author bai
 * @date 2024/06/08
 */
@Slf4j
@RestController
@Api(value = "v3-技师与物料关系表", tags = { "v3-技师与物料关系表" })
@RequestMapping(value = "/blArtificerSelfGoods")
public class BlArtificerSelfGoodsController {

    @Autowired
    private BlArtificerSelfGoodsService service;

    @GetMapping("/list")
    @ApiOperation("获取数据列表")
    public Result list(BlArtificerSelfGoods entity, Integer page,Integer limit){
        QueryWrapper<BlArtificerSelfGoods> qw = new QueryWrapper<>();

        if(entity != null) {
            //技师
            qw.lambda().eq(entity.getArtificerId() != null, BlArtificerSelfGoods::getArtificerId, entity.getArtificerId());
        }

        qw.orderByAsc("create_time", "update_time");
        IPage<BlArtificerSelfGoods> pageList = service.page(new Page<>(page,limit),qw);
        return Result.success().put("data",pageList);
    }

    @PostMapping(value = "/addList")
    @ApiOperation("新增")
    public Result add(@RequestBody List<BlArtificerSelfGoods> list){
        try {
            service.editWarehouse(list);
            return Result.success();
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return Result.error(e.getMessage());
        }
    }

    @PostMapping(value = "/update")
    @ApiOperation("修改")
    public Result update(@RequestBody BlArtificerSelfGoods entity){
        entity.setUpdateTime(DateUtil.now());
        try {
            service.editWarehouseById(entity.getId(), entity.getChangeNum(), entity.getRemark());
            return Result.success();
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return Result.error(e.getMessage());
        }
    }

    @RequestMapping("/info/{id}")
    public Result info(@PathVariable("id") Long id){
        BlArtificerSelfGoods data = service.getById(id);
        return Result.success().put("data", data);
    }

//    @DeleteMapping(value = "/deleteById")
//    @ApiOperation("删除")
//    public Result deleteById(Long id){
//        service.removeById(id);
//        return Result.success();
//    }
//
//    @DeleteMapping(value = "/deleteByIds")
//    @ApiOperation("批量删除")
//    public Result deleteByIds(String id){
//        if(StringUtils.isNotBlank(id)){
//            String[] ids = StringUtils.split(id,",");
//            service.removeByIds(Arrays.asList(ids));
//        }
//        return Result.success();
//    }


}