package com.gxwebsoft.tower.controller;

import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.Company;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.tower.entity.TowerEquipment;
import com.gxwebsoft.tower.entity.TowerHistorySettle;
import com.gxwebsoft.tower.entity.TowerProject;
import com.gxwebsoft.tower.service.TowerRentRecordService;
import com.gxwebsoft.tower.entity.TowerRentRecord;
import com.gxwebsoft.tower.param.TowerRentRecordParam;
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-13 00:42:42
 */
@Api(tags = "租单记录管理")
@RestController
@RequestMapping("/api/tower/tower-rent-record")
public class TowerRentRecordController extends BaseController {
    @Resource
    private TowerRentRecordService towerRentRecordService;

    @PreAuthorize("hasAuthority('tower:towerRentRecord:list')")
    @OperationLog
    @ApiOperation("分页查询租单记录")
    @GetMapping("/page")
    public ApiResult<PageResult<TowerRentRecord>> page(TowerRentRecordParam param) {
        MPJLambdaWrapper<TowerRentRecord> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(TowerRentRecord.class)
                .select(TowerProject::getProjectName)
                .select(Company::getCompanyName)
                .selectAs(TowerEquipment::getName, TowerEquipment::getEquipmentName)
                .selectAs(TowerEquipment::getModel, TowerEquipment::getEquipmentModel)
                .select(TowerEquipment::getEquipmentNo)
                .select(TowerEquipment::getFactoryNo)
                .select(TowerEquipment::getFilingNo)
                .leftJoin(TowerProject.class, TowerProject::getProjectId, TowerRentRecord::getProjectId)
                .leftJoin(Company.class, Company::getCompanyId, TowerRentRecord::getCompanyId)
                .leftJoin(TowerEquipment.class, TowerEquipment::getEquipmentId, TowerRentRecord::getEquipmentId);
        PageParam<TowerRentRecord, TowerRentRecordParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerRentRecordService.page(page, wrapper));
        // 使用关联查询
        //return success(towerRentRecordService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerRentRecord:list')")
    @OperationLog
    @ApiOperation("查询全部租单记录")
    @GetMapping()
    public ApiResult<List<TowerRentRecord>> list(TowerRentRecordParam param) {
        PageParam<TowerRentRecord, TowerRentRecordParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerRentRecordService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(towerRentRecordService.listRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerRentRecord:list')")
    @OperationLog
    @ApiOperation("根据id查询租单记录")
    @GetMapping("/{id}")
    public ApiResult<TowerRentRecord> get(@PathVariable("id") Integer id) {
        return success(towerRentRecordService.getById(id));
        // 使用关联查询
        //return success(towerRentRecordService.getByIdRel(id));
    }

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

    @PreAuthorize("hasAuthority('tower:towerRentRecord:update')")
    @OperationLog
    @ApiOperation("修改租单记录")
    @PutMapping()
    public ApiResult<?> update(@RequestBody TowerRentRecord towerRentRecord) {
        if (towerRentRecordService.updateById(towerRentRecord)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

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

    @PreAuthorize("hasAuthority('tower:towerRentRecord:update')")
    @OperationLog
    @ApiOperation("批量修改租单记录")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<TowerRentRecord> batchParam) {
        if (batchParam.update(towerRentRecordService, "rent_record_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

}
