package com.sy.coinage.workshop.controller;

import com.sy.coinage.core.constants.Constants;
import com.sy.coinage.sys.model.Dict;
import com.sy.coinage.sys.service.IDictService;
import com.sy.coinage.workshop.service.IProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

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

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sy.coinage.core.annotation.ActiveFastJsonProfile;
import com.sy.coinage.core.annotation.CurrentUser;
import com.sy.coinage.core.aop.annotation.Permission;
import com.sy.coinage.core.aop.annotation.Operation;
import com.sy.coinage.core.controller.BaseController;
import com.sy.coinage.core.interceptor.Create;
import com.sy.coinage.core.interceptor.Update;
import com.sy.coinage.core.req.*;
import com.sy.coinage.core.vo.*;

import com.sy.coinage.workshop.model.*;
import com.sy.coinage.workshop.service.IScreenDataService;
import com.sy.coinage.workshop.vo.*;
import com.sy.coinage.workshop.req.*;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

@Api(value = "大屏数据管理表接口", tags = {"大屏数据管理表接口"})
@RestController
@RequestMapping(value = "/screenData")
public class ScreenDataController extends BaseController {

    @Autowired
    private IScreenDataService screenDataService;

    @Autowired
    private IProductService productService;

    @Autowired
    private IDictService dictService;

    @ApiOperation(value = "大屏数据管理表分页列表")
    @PostMapping("/page")
    @ActiveFastJsonProfile(groups = "list", clazz = ScreenData.class)
    public IPage<ScreenData> page(@RequestBody @ApiParam(value = "列表查询请求bean") ScreenDataListReq req, @CurrentUser CacheUserVO user) {
        LambdaQueryWrapper<ScreenData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq((req.getSecuritiesId() != null && req.getSecuritiesId() > 0), ScreenData::getSecuritiesId, req.getSecuritiesId());

        Page<ScreenData> page = new Page<>(req.getPage(), req.getRows());
        IPage<ScreenData> data = this.screenDataService.page(page, queryWrapper);
        return data;
    }

    @ApiOperation(value = "大屏数据管理表列表")
    @PostMapping("/list")
    @ActiveFastJsonProfile(groups = "list", clazz = ScreenData.class)
    public List<ScreenData> list(@RequestBody @ApiParam(value = "列表查询请求bean") ScreenDataListReq req, @CurrentUser CacheUserVO user) {
        LambdaQueryWrapper<ScreenData> queryWrapper = new LambdaQueryWrapper<>();
        List<ScreenData> data = this.screenDataService.list(queryWrapper);
        return data;
    }

    @ApiOperation(value = "获取大屏数据管理表详情")
    @PostMapping("/get")
    @ActiveFastJsonProfile(groups = "get", clazz = ScreenData.class)
    public ScreenDataVO get(@RequestBody @ApiParam(value = "主键请求bean") @Valid IdReq req) {
        ScreenDataVO data = null;
        ScreenData model = this.screenDataService.getById(req.getId());
        data = new ScreenDataVO();
        BeanUtils.copyProperties(model, data);
        return data;
    }

    // @ApiOperation(value = "大屏数据管理表选项")
    // @GetMapping("/opt")
    // public List<OptVO> opt(){
    // List<OptVO> data = this.screenDataService.opt();
    //   return data;
    // }

    @ApiOperation(value = "新增大屏数据管理表")
    @Operation(value = "新增大屏数据管理表")
    @PostMapping("/add")
//    @Permission("screenData:add")
    public Object add(@RequestBody @ApiParam(value = "新增请求bean") @Validated({Create.class}) ScreenDataReq req, @CurrentUser CacheUserVO user) {
        //  this.screenDataService.add(req, user);
        ScreenData model = new ScreenData();
        BeanUtils.copyProperties(req, model);
        model.setStatus(Constants.STATUS_USABLE);
        Dict dict = this.dictService.getByVal("wareOpt", req.getWareId(), null);
        model.setWareCn(dict.getName());
        Product product = this.productService.getByCache(req.getSecuritiesId());
        model.setSecurities(product.getSecurities());
        this.screenDataService.save(model);
        return "";
    }

    @ApiOperation(value = "修改大屏数据管理表")
    @Operation(value = "修改大屏数据管理表")
    @PostMapping("/update")
//    @Permission("screenData:update")
    public Object update(@RequestBody @ApiParam(value = "修改请求bean") @Validated({Update.class}) ScreenDataReq req, @CurrentUser CacheUserVO user) {
        //  this.screenDataService.update(req, user)
        ScreenData model = this.screenDataService.getById(req.getId());
        BeanUtils.copyProperties(req, model);
        Dict dict = this.dictService.getByVal("wareOpt", req.getWareId(), null);
        model.setWareCn(dict.getName());
        Product product = this.productService.getByCache(req.getSecuritiesId());
        model.setSecurities(product.getSecurities());
        this.screenDataService.updateById(model);
        return "";
    }

    @ApiOperation(value = "修改大屏数据管理表状态")
    @Operation(value = "修改大屏数据管理表状态")
    @PostMapping("/status")
//      @Permission("screenData:update")
    public Integer status(@RequestBody @Valid IdReq req, @CurrentUser CacheUserVO user) {
        ScreenData model = this.screenDataService.getById(req.getId());
        if (null != model.getStatus() && model.getStatus().intValue() == Constants.STATUS_FORBIDDEN) {
            model.setStatus(Constants.STATUS_USABLE);
        } else {
            model.setStatus(Constants.STATUS_FORBIDDEN);
        }
        this.screenDataService.updateById(model);
        return model.getStatus();
    }


    @ApiOperation(value = "删除大屏数据管理表")
    @Operation(value = "删除大屏数据管理表")
    @PostMapping("/delete")
//    @Permission("screenData:delete")
    public Object delete(@RequestBody @ApiParam(value = "主键请求bean") @Valid IdReq req, @CurrentUser CacheUserVO user) {
        ScreenData model = this.screenDataService.getById(req.getId());
        // 验证是否相同租户
        //    model.setDelFlag(Constants.STATUS_FORBIDDEN);
        //    this.screenDataService.updateById(model);
        this.screenDataService.removeById(req.getId());
        return "";
    }

}
