package com.gxwebsoft.tower.controller;

import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.tower.entity.*;
import com.gxwebsoft.tower.service.TowerHistorySettleService;
import com.gxwebsoft.tower.param.TowerHistorySettleParam;
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-06-12 16:40:25
 */
@Api(tags = "管理")
@RestController
@RequestMapping("/api/tower/tower-history-settle")
public class TowerHistorySettleController extends BaseController {
    @Resource
    private TowerHistorySettleService towerHistorySettleService;

    @PreAuthorize("hasAuthority('tower:towerHistorySettle:list')")
    @OperationLog
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public ApiResult<PageResult<TowerHistorySettle>> page(TowerHistorySettleParam param) {
        MPJLambdaWrapper<TowerHistorySettle> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(TowerHistorySettle.class)
                .select(TowerProject::getProjectName)
                .selectAs(TowerEquipment::getName, TowerEquipment::getEquipmentName)
                .selectAs(TowerEquipment::getModel, TowerEquipment::getEquipmentModel)
                .select(TowerEquipment::getEquipmentNo)
                .select(TowerEquipment::getFactoryNo)
                .select(TowerEquipment::getFilingNo)
                .leftJoin(TowerProject.class, TowerProject::getProjectId, TowerHistorySettle::getProjectId)
                .leftJoin(TowerEquipment.class, TowerEquipment::getEquipmentId, TowerHistorySettle::getEquipmentId);

        PageParam<TowerHistorySettle, TowerHistorySettleParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerHistorySettleService.page(page, wrapper));
        // 使用关联查询
        //return success(towerHistorySettleService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerHistorySettle:list')")
    @OperationLog
    @ApiOperation("查询全部")
    @GetMapping()
    public ApiResult<List<TowerHistorySettle>> list(TowerHistorySettleParam param) {
        PageParam<TowerHistorySettle, TowerHistorySettleParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerHistorySettleService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(towerHistorySettleService.listRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerHistorySettle:list')")
    @OperationLog
    @ApiOperation("根据id查询")
    @GetMapping("/{id}")
    public ApiResult<TowerHistorySettle> get(@PathVariable("id") Integer id) {
        return success(towerHistorySettleService.getById(id));
        // 使用关联查询
        //return success(towerHistorySettleService.getByIdRel(id));
    }

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

    @PreAuthorize("hasAuthority('tower:towerHistorySettle:update')")
    @OperationLog
    @ApiOperation("修改")
    @PutMapping()
    public ApiResult<?> update(@RequestBody TowerHistorySettle towerHistorySettle) {
        if (towerHistorySettleService.updateById(towerHistorySettle)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

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

    @PreAuthorize("hasAuthority('tower:towerHistorySettle:update')")
    @OperationLog
    @ApiOperation("批量修改")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<TowerHistorySettle> batchParam) {
        if (batchParam.update(towerHistorySettleService, "history_settle_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

}
