package com.gxwebsoft.tower.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.tower.service.TowerWorksheetCheckService;
import com.gxwebsoft.tower.entity.TowerWorksheetCheck;
import com.gxwebsoft.tower.param.TowerWorksheetCheckParam;
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-07-04 01:24:45
 */
@Api(tags = "派单现场核验管理")
@RestController
@RequestMapping("/api/tower/tower-worksheet-check")
public class TowerWorksheetCheckController extends BaseController {
    @Resource
    private TowerWorksheetCheckService towerWorksheetCheckService;

    @PreAuthorize("hasAuthority('tower:towerWorksheetCheck:list')")
    @OperationLog
    @ApiOperation("分页查询派单现场核验")
    @GetMapping("/page")
    public ApiResult<PageResult<TowerWorksheetCheck>> page(TowerWorksheetCheckParam param) {
        PageParam<TowerWorksheetCheck, TowerWorksheetCheckParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerWorksheetCheckService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(towerWorksheetCheckService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerWorksheetCheck:list')")
    @OperationLog
    @ApiOperation("查询全部派单现场核验")
    @GetMapping()
    public ApiResult<List<TowerWorksheetCheck>> list(TowerWorksheetCheckParam param) {
        PageParam<TowerWorksheetCheck, TowerWorksheetCheckParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerWorksheetCheckService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(towerWorksheetCheckService.listRel(param));
    }

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

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

    @PreAuthorize("hasAuthority('tower:towerWorksheetCheck:update')")
    @OperationLog
    @ApiOperation("修改派单现场核验")
    @PutMapping()
    public ApiResult<?> update(@RequestBody TowerWorksheetCheck towerWorksheetCheck) {
        if (towerWorksheetCheckService.updateById(towerWorksheetCheck)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

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

    @PreAuthorize("hasAuthority('tower:towerWorksheetCheck:update')")
    @OperationLog
    @ApiOperation("批量修改派单现场核验")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<TowerWorksheetCheck> batchParam) {
        if (batchParam.update(towerWorksheetCheckService, "check_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

}
