package com.sy.coinage.workshop.controller;

import cn.hutool.core.date.DateUtil;
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.IWorkshopLogService;
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 = "/workshopLog")
public class WorkshopLogController extends BaseController {

    @Autowired
    private IWorkshopLogService workshopLogService;

    /**
     * 记录每笔出入库流水表(车间出入库记录)分页列表
     */
    @ApiOperation(value = "记录每笔出入库流水表(车间出入库记录)分页列表")
    @PostMapping("/list")
    @ActiveFastJsonProfile(groups = "list", clazz = WorkshopLog.class)
    public IPage<WorkshopLog> list(@RequestBody WorkshopLogListReq req, @CurrentUser CacheUserVO user) {
        LambdaQueryWrapper<WorkshopLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(req.getSecuritiesId() != null && req.getSecuritiesId() > 0, WorkshopLog::getSecuritiesId, req.getSecuritiesId());
        queryWrapper.eq(req.getOpType() != null && req.getOpType() > 0, WorkshopLog::getOpType, req.getOpType());
        if (req.getCreateTime() != null && StringUtils.isNotBlank(String.valueOf(req.getCreateTime()))) {
            queryWrapper.between(WorkshopLog::getCreateTime, DateUtils.getDayStartTime(req.getCreateTime()), DateUtils.getDayEndTime(req.getStartDate()));
        }
        queryWrapper.orderByDesc(WorkshopLog::getCreateTime);
        Page<WorkshopLog> page = new Page<>(req.getPage(), req.getRows());
        IPage<WorkshopLog> data = this.workshopLogService.page(page, queryWrapper);
        return data;
    }

    // /**
    // * -记录每笔出入库流水表(车间出入库记录)分页列表
    // */
    // @ApiOperation(value = "记录每笔出入库流水表(车间出入库记录)分页列表")
    // @PostMapping("/list")
    // @ActiveFastJsonProfile(groups = "list",clazz = WorkshopLog.class)
    // public IPage<WorkshopLogVO> list(@RequestBody WorkshopLogListReq req) {
    // IPage<WorkshopLogVO> data = this.workshopLogService.queryPageList(req);
    // return data;
    // }

    /**
     * 记录每笔出入库流水表(车间出入库记录)详情
     */
    @ApiOperation(value = "记录每笔出入库流水表(车间出入库记录)-通过ID查询单条数据")
    @PostMapping("/get")
    @ActiveFastJsonProfile(groups = "get", clazz = WorkshopLog.class)
    public WorkshopLogVO get(@RequestBody @ApiParam(value = "主键请求bean") @Valid IdReq req) {
        WorkshopLogVO data = null;
        WorkshopLog model = this.workshopLogService.getById(req.getId());
        data = new WorkshopLogVO();
        BeanUtils.copyProperties(model, data);
        return data;
    }

    /**
     * 记录每笔出入库流水表(车间出入库记录)列表项
     */
    // @GetMapping("/opt")
    // public List<OptVO> opt(){
    // List<OptVO> data = this.workshopLogService.opt();
    // return data;
    // }

    /**
     * 新增记录每笔出入库流水表(车间出入库记录)
     *
     * @param req {@link WorkshopLogReq}
     * @return
     */
    @ApiOperation(value = "新增记录每笔出入库流水表(车间出入库记录)")
    @Operation(value = "新增记录每笔出入库流水表(车间出入库记录)")
    @PostMapping("/add")
    @Permission("workshopLog:add")
    public Object add(@RequestBody @ApiParam(value = "新增请求bean") @Validated({Create.class}) WorkshopLogReq req,
                      @CurrentUser CacheUserVO user) {
        WorkshopLog model = new WorkshopLog();
        BeanUtils.copyProperties(req, model);
        // model.setCreateBy(user.getName());
        // model.setUpdateBy(user.getName());
        this.workshopLogService.save(model);
        return "";
    }

    /**
     * 修改记录每笔出入库流水表(车间出入库记录)
     */
    @ApiOperation(value = "修改记录每笔出入库流水表(车间出入库记录)")
    @Operation(value = "修改记录每笔出入库流水表(车间出入库记录)")
    @PostMapping("/update")
    @Permission("workshopLog:update")
    public Object update(@RequestBody @ApiParam(value = "修改请求bean") @Validated({Update.class}) WorkshopLogReq req,
                         @CurrentUser CacheUserVO user) {
        WorkshopLog model = this.workshopLogService.getById(req.getId());
        BeanUtils.copyProperties(req, model);
        // model.setUpdateBy(user.getName());
        this.workshopLogService.updateById(model);
        return "";
    }

}
