package com.gxwebsoft.tower.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.tower.service.TowerWorksheetAuditService;
import com.gxwebsoft.tower.entity.TowerWorksheetAudit;
import com.gxwebsoft.tower.param.TowerWorksheetAuditParam;
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 2023-08-21 00:46:43
 */
@Api(tags = "派单审核管理")
@RestController
@RequestMapping("/api/tower/tower-worksheet-audit")
public class TowerWorksheetAuditController extends BaseController {
    @Resource
    private TowerWorksheetAuditService towerWorksheetAuditService;

    @PreAuthorize("hasAuthority('tower:towerWorksheetAudit:list')")
    @OperationLog
    @ApiOperation("分页查询派单审核")
    @GetMapping("/page")
    public ApiResult<PageResult<TowerWorksheetAudit>> page(TowerWorksheetAuditParam param) {
        PageParam<TowerWorksheetAudit, TowerWorksheetAuditParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerWorksheetAuditService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(towerWorksheetAuditService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerWorksheetAudit:list')")
    @OperationLog
    @ApiOperation("查询全部派单审核")
    @GetMapping()
    public ApiResult<List<TowerWorksheetAudit>> list(TowerWorksheetAuditParam param) {
        PageParam<TowerWorksheetAudit, TowerWorksheetAuditParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerWorksheetAuditService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(towerWorksheetAuditService.listRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerWorksheetAudit:list')")
    @OperationLog
    @ApiOperation("根据id查询派单审核")
    @GetMapping("/{id}")
    public ApiResult<TowerWorksheetAudit> get(@PathVariable("id") Integer id) {
        return success(towerWorksheetAuditService.getById(id));
        // 使用关联查询
        //return success(towerWorksheetAuditService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('tower:towerWorksheetAudit:save')")
    @OperationLog
    @ApiOperation("添加派单审核")
    @PostMapping()
    public ApiResult<?> save(@RequestBody TowerWorksheetAudit towerWorksheetAudit) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          towerWorksheetAudit.setUserId(loginUser.getUserId());
        }
        if (towerWorksheetAuditService.save(towerWorksheetAudit)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerWorksheetAudit:update')")
    @OperationLog
    @ApiOperation("修改派单审核")
    @PutMapping()
    public ApiResult<?> update(@RequestBody TowerWorksheetAudit towerWorksheetAudit) {
        if (towerWorksheetAuditService.updateById(towerWorksheetAudit)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('tower:towerWorksheetAudit:remove')")
    @OperationLog
    @ApiOperation("删除派单审核")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (towerWorksheetAuditService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('tower:towerWorksheetAudit:save')")
    @OperationLog
    @ApiOperation("批量添加派单审核")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<TowerWorksheetAudit> list) {
        if (towerWorksheetAuditService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerWorksheetAudit:update')")
    @OperationLog
    @ApiOperation("批量修改派单审核")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<TowerWorksheetAudit> batchParam) {
        if (batchParam.update(towerWorksheetAuditService, "worksheet_audit_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('tower:towerWorksheetAudit:remove')")
    @OperationLog
    @ApiOperation("批量删除派单审核")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (towerWorksheetAuditService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
