package com.gxwebsoft.tower.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.tower.service.TowerPurchaseService;
import com.gxwebsoft.tower.entity.TowerPurchase;
import com.gxwebsoft.tower.param.TowerPurchaseParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 采购入库表控制器
 *
 * @author 科技小王子
 * @since 2024-02-05 20:53:32
 */
@Api(tags = "采购入库表管理")
@RestController
@RequestMapping("/api/tower/tower-purchase")
public class TowerPurchaseController extends BaseController {
    @Resource
    private TowerPurchaseService towerPurchaseService;

    @PreAuthorize("hasAuthority('tower:towerPurchase:list')")
    @OperationLog
    @ApiOperation("分页查询采购入库表")
    @GetMapping("/page")
    public ApiResult<PageResult<TowerPurchase>> page(TowerPurchaseParam param) {
        PageParam<TowerPurchase, TowerPurchaseParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerPurchaseService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(towerPurchaseService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerPurchase:list')")
    @OperationLog
    @ApiOperation("查询全部采购入库表")
    @GetMapping()
    public ApiResult<List<TowerPurchase>> list(TowerPurchaseParam param) {
        PageParam<TowerPurchase, TowerPurchaseParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerPurchaseService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(towerPurchaseService.listRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerPurchase:list')")
    @OperationLog
    @ApiOperation("根据id查询采购入库表")
    @GetMapping("/{id}")
    public ApiResult<TowerPurchase> get(@PathVariable("id") Integer id) {
        return success(towerPurchaseService.getById(id));
        // 使用关联查询
        //return success(towerPurchaseService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('tower:towerPurchase:save')")
    @OperationLog
    @ApiOperation("添加采购入库表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody TowerPurchase towerPurchase) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          towerPurchase.setUserId(loginUser.getUserId());
        }
        if (towerPurchaseService.save(towerPurchase)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerPurchase:update')")
    @OperationLog
    @ApiOperation("修改采购入库表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody TowerPurchase towerPurchase) {
        if (towerPurchaseService.updateById(towerPurchase)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('tower:towerPurchase:remove')")
    @OperationLog
    @ApiOperation("删除采购入库表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (towerPurchaseService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('tower:towerPurchase:save')")
    @OperationLog
    @ApiOperation("批量添加采购入库表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<TowerPurchase> list) {
        if (towerPurchaseService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerPurchase:update')")
    @OperationLog
    @ApiOperation("批量修改采购入库表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<TowerPurchase> batchParam) {
        if (batchParam.update(towerPurchaseService, "purchase_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('tower:towerPurchase:remove')")
    @OperationLog
    @ApiOperation("批量删除采购入库表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (towerPurchaseService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
