package com.yuchen.saas.park.service.controller.rfid;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.yuchen.saas.park.api.entity.park.PmgBatRfidInfo;
import com.yuchen.saas.park.service.service.PmgBatRfidInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.nest.springwrap.core.mp.support.Condition;
import org.nest.springwrap.core.mp.support.Query;
import org.nest.springwrap.core.tool.api.R;
import org.nest.springwrap.core.tool.utils.Func;
import org.nest.springwrap.core.tool.utils.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * RFID标签管理
 */
@RestController
@AllArgsConstructor
@RequestMapping("/rfidInfo")
@Api(value = "RFID标签管理", tags = "RFID标签管理接口")
public class PmgBatRfidInfoController {
    @Autowired
    private PmgBatRfidInfoService pmgBatRfidInfoService;


    @Autowired
    public void cacheEpcList() {
        List<String> epList = pmgBatRfidInfoService.getEpc();
    }

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入对象信息")
    public R<PmgBatRfidInfo> detail(PmgBatRfidInfo pmgBatRfidInfo) {
        PmgBatRfidInfo detail = pmgBatRfidInfoService.getOne(Condition.getQueryWrapper(pmgBatRfidInfo));
        return R.data(detail);
    }

    /**
     * 根据EPC查询
     */
    @GetMapping("/getByEpc")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入对象信息")
    public R<PmgBatRfidInfo> getByEpc(@RequestParam("epc") String epc) {
        PmgBatRfidInfo pmgBatRfidInfo = pmgBatRfidInfoService.getOne(Wrappers.<PmgBatRfidInfo>query().lambda()
                .eq(PmgBatRfidInfo::getEpc,epc).eq(PmgBatRfidInfo::getEpc, 0));
        return R.data(pmgBatRfidInfo);
    }

    /**
     * 获取epc列表
     */
    @GetMapping("/getEpcList")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入对象信息")
    public R<List<String>> getEpcList() {
        return R.data(pmgBatRfidInfoService.getEpc());
    }

    /**
     * 分页
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页", notes = "传入对象信息")
    public R<IPage<PmgBatRfidInfo>> list(PmgBatRfidInfo pmgBatRfidInfo, Query query) {
        IPage<PmgBatRfidInfo> pages = pmgBatRfidInfoService.page(Condition.getPage(query),Condition.getQueryWrapper(pmgBatRfidInfo));
        return  R.data(pages);
    }
    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "传入对象信息")
    public R save(@Valid @RequestBody PmgBatRfidInfo pmgBatRfidInfo) {
        if(pmgBatRfidInfoService.existEpc(pmgBatRfidInfo.getEpc())) {
            return R.fail("标签信息已存在");
        }
        boolean b = pmgBatRfidInfoService.save(pmgBatRfidInfo);
        return R.status(b);
    }
    /**
     * 新增或修改
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "新增或修改", notes = "传入对象信息")
    public R submit(@Valid @RequestBody PmgBatRfidInfo pmgBatRfidInfo) {
        if(pmgBatRfidInfoService.existEpc(pmgBatRfidInfo.getEpc())) {
            return R.fail("标签信息已存在");
        }
        boolean b = pmgBatRfidInfoService.saveOrUpdate(pmgBatRfidInfo);
        return R.status(b);
    }
    /**
     * 修改
     */
    @PutMapping("/update")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "修改", notes = "传入对象信息")
    public R update(@Valid @RequestBody PmgBatRfidInfo pmgBatRfidInfo) {
        if(ObjectUtil.isEmpty(pmgBatRfidInfo.getPlaceSrc())){
            pmgBatRfidInfo.setPlaceSrc("");
        }
        PmgBatRfidInfo pmgBatRfidInfoOld = pmgBatRfidInfoService.getById(pmgBatRfidInfo.getId());
        if (!pmgBatRfidInfo.getEpc().equals(pmgBatRfidInfoOld.getEpc())) {
            //如果有修改epc，检查新值的唯一性
            if(pmgBatRfidInfoService.existEpc(pmgBatRfidInfo.getEpc())) {
                return R.fail("标签信息已存在");
            }
        }
        return R.status(pmgBatRfidInfoService.updateById(pmgBatRfidInfo));
    }
    /**
     * 删除
     */
    @DeleteMapping("/remove")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "逻辑删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        boolean b =pmgBatRfidInfoService.deleteLogic(Func.toLongList(ids));
        return R.status(b);
    }
}
