package com.sy.coinage.workshop.controller;

import com.sy.coinage.core.util.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
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.ICheckService;
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.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * -Controller
 */
@Api(value = "接口", tags = {"接口"})
@RestController
@RequestMapping(value = "/check")
public class CheckController extends BaseController {

    @Autowired
    private ICheckService checkService;

    /**
     * 分页列表
     */
    @ApiOperation(value = "分页列表")
    @PostMapping("/page")
    @ActiveFastJsonProfile(groups = "list", clazz = Check.class)
    public IPage<Check> page(@RequestBody CheckListReq req, @CurrentUser CacheUserVO user) {
        LambdaQueryWrapper<Check> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(req.getDeptId() != null && req.getDeptId() > 0, Check::getDeptId, req.getDeptId());
        queryWrapper.eq(req.getSecuritiesId() != null && req.getSecuritiesId() > 0, Check::getSecuritiesId, req.getSecuritiesId());
        if (req.getStartDate() != null) {
            queryWrapper.ge(Check::getCreateTime, DateUtils.getDayStartTime(req.getStartDate()));
        }
        if (req.getEndDate() != null) {
            queryWrapper.le(Check::getCreateTime, DateUtils.getDayEndTime(req.getEndDate()));
        }
        queryWrapper.le(req.getStatus() != null && req.getStatus() > 0, Check::getStatus, req.getStatus());
        queryWrapper.like(StringUtils.isNotBlank(req.getOrderNo()), Check::getOrderNo, req.getOrderNo());
        queryWrapper.le(req.getWareId() != null && req.getWareId() > 0, Check::getWareId, req.getWareId());
        queryWrapper.orderByDesc(Check::getId);
        Page<Check> page = new Page<>(req.getPage(), req.getRows());
        IPage<Check> data = this.checkService.page(page, queryWrapper);
        return data;
    }

    /**
     * 详情
     */
    @ApiOperation(value = "-通过ID查询单条数据")
    @PostMapping("/get")
    @ActiveFastJsonProfile(groups = "get", clazz = Check.class)
    public CheckVO get(@RequestBody @ApiParam(value = "主键请求bean") @Valid IdReq req) {
        CheckVO data = null;
        Check model = this.checkService.getById(req.getId());
        data = new CheckVO();
        BeanUtils.copyProperties(model, data);
        return data;
    }

    /**
     * 新增
     *
     * @param req {@link CheckReq}
     * @return
     */
    @ApiOperation(value = "新增")
    @Operation(value = "新增")
    @PostMapping("/add")
    @Permission("check:add")
    public Object add(@RequestBody @ApiParam(value = "新增请求bean") @Validated({Create.class}) CheckReq req, @CurrentUser CacheUserVO user) {
        Check model = new Check();
        BeanUtils.copyProperties(req, model);
        // model.setCreateBy(user.getName());
        // model.setUpdateBy(user.getName());
        this.checkService.save(model);
        return "";
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除")
    @Operation(value = "删除")
    @PostMapping("/delete")
    @Permission("check:delete")
    public Object delete(@RequestBody @ApiParam(value = "主键请求bean") @Valid IdReq req, @CurrentUser CacheUserVO user) {
        Check model = this.checkService.getById(req.getId());
        // model.setDelFlag(Constants.STATUS_FORBIDDEN);
        // this.checkService.updateById(model);
        this.checkService.removeById(req.getId());
        return "";
    }

}
