package com.ssm.controller;

import javax.persistence.Column;
import javax.persistence.Id;

import com.github.pagehelper.PageInfo;
import com.ssm.filter.LoginInterceptor;
import com.ssm.manage.entity.WorkShop;
import com.ssm.manage.response.GenericResponse;
import com.ssm.manage.response.PageResponse;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.response.StaffResponse;
import com.ssm.manage.service.WorkShopService;
import dto.WorkShopDto;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import com.ssm.core.utils.SnowflakeUtil;

import java.util.Date;
import java.util.Random;

/**
 * (WorkShop)表控制层
 *
 * @author makejava
 * @since 2023-12-16 11:04:55
 */
@RestController
@RequestMapping("workShop")
@Slf4j
@Api(tags = "车间管理")
public class WorkShopController {
    /**
     * 服务对象
     */
    @Autowired
    private WorkShopService workShopService;

/**
 * 通过主键查询单条数据
 *
 * @param id 主键
 * @return
 @GetMapping("{id}")
 @ApiOperation("通过主键查询单条数据")
 //@RequiresPermissions("system:workShop:query")
 public ResultVo get(@PathVariable Serializable id) {
 return this.workShopService.queryById(id);
 }
 */


    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @GetMapping
    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", defaultValue = "10", required = true)
    })
//@RequiresPermissions("system:workShop:query")
    public PageResponse<WorkShop> listPage(WorkShopDto dto) {
        PageResponse<WorkShop> pageResponse = new PageResponse<>();
        PageInfo<WorkShop> pageInfo = new PageInfo<>();
        try {
            pageInfo = this.workShopService.selectPage(dto);
        } catch (Exception e) {
            log.error("SsmCollectionTypeController.selectPage", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setPageInfo(pageInfo);
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        return pageResponse;
    }

    /**
     * 新增
     *
     * @param workShop
     * @return
     */
    @PostMapping
    @ApiOperation("新增")
//@RequiresPermissions("system:workShop:add")
    public GenericResponse add(@RequestBody WorkShop workShop) {
        GenericResponse genericResponse = new GenericResponse();
        int i = 0;
        StaffResponse staffResponse = LoginInterceptor.getCurrentUserInfo();
        LoginInterceptor.clearThreadLocal();
        try {
            workShop.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
            workShop.setInputDate(new Date());
            workShop.setInputName(staffResponse.getStaffName());
            workShop.setInputId(staffResponse.getId());
            i = this.workShopService.save(workShop);
        } catch (Exception e) {
            log.error("SsmCollectionTypeController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

    /**
     * 编辑
     *
     * @param workShop
     * @return
     */
    @PutMapping
    @ApiOperation("编辑")
//@RequiresPermissions("system:workShop:edit")
    public GenericResponse edit(@RequestBody WorkShop workShop) {
        GenericResponse genericResponse = new GenericResponse();
        int i = 0;
        try {
            i = this.workShopService.updateByPrimaryKey(workShop);
        } catch (Exception e) {
            log.error("SsmCollectionTypeController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

    /**
     * 根据主键删除
     *
     * @param id 主键
     * @return
     */
    @DeleteMapping("{id}")
    @ApiOperation("根据主键删除")
//@RequiresPermissions("system:workShop:delete")
    public GenericResponse delete(@PathVariable Long id) {
        GenericResponse genericResponse = new GenericResponse();
        int i = 0;
        try {
            WorkShop workShop = new WorkShop();
            workShop.setId(id);
            i = this.workShopService.delete(workShop);
        } catch (Exception e) {
            log.error("SsmCollectionTypeController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

}
