package com.gxwebsoft.tower.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.*;
import com.gxwebsoft.tower.mapper.TowerContractMapper;
import com.gxwebsoft.tower.service.TowerContractService;
import com.gxwebsoft.tower.param.TowerContractParam;
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 com.gxwebsoft.tower.service.TowerContractSettleEquipmentService;
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.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 合同存档控制器
 *
 * @author 科技小王子
 * @since 2023-06-08 22:36:47
 */
@Api(tags = "合同存档管理")
@RestController
@RequestMapping("/api/tower/tower-contract")
public class TowerContractController extends BaseController {
    @Resource
    private TowerContractService towerContractService;
    @Resource
    private TowerContractSettleEquipmentService towerContractSettleEquipmentService;

    @PreAuthorize("hasAuthority('tower:towerContract:list')")
    @OperationLog
    @ApiOperation("分页查询合同存档")
    @GetMapping("/page")
    public ApiResult<PageResult<TowerContract>> page(TowerContractParam param) {
        MPJLambdaWrapper<TowerContract> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(TowerContract.class)
                .selectAs(TowerCustomer::getName, TowerContract::getCustomerName)
                .selectAs(Company::getCompanyName, TowerContract::getCompanyName)
                .leftJoin(TowerCustomer.class, TowerCustomer::getCustomerId, TowerContract::getCustomerId)
                .leftJoin(Company.class, Company::getCompanyId, TowerContract::getCompanyId);
        PageParam<TowerContract, TowerContractParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        PageResult<TowerContract> pageRes = towerContractService.pageRel(param);
        return success(pageRes);
//        return success(towerContractService.page(page, wrapper));
        // 使用关联查询
        //return success(towerContractService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerContract:list')")
    @OperationLog
    @ApiOperation("查询全部合同存档")
    @GetMapping()
    public ApiResult<List<TowerContract>> list(TowerContractParam param) {
        PageParam<TowerContract, TowerContractParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerContractService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(towerContractService.listRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerContract:list')")
    @OperationLog
    @ApiOperation("根据id查询合同存档")
    @GetMapping("/{id}")
    public ApiResult<TowerContract> get(@PathVariable("id") Integer id) {
        return success(towerContractService.getById(id));
        // 使用关联查询
        //return success(towerContractService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('tower:towerContract:save')")
    @OperationLog
    @ApiOperation("添加合同存档")
    @PostMapping()
    public ApiResult<?> save(@RequestBody TowerContract towerContract) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
            towerContract.setUserId(loginUser.getUserId());
        }
        if (towerContractService.save(towerContract)) {
            return success(towerContract.getContractId());
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerContract:update')")
    @OperationLog
    @ApiOperation("修改合同存档")
    @PutMapping()
    public ApiResult<?> update(@RequestBody TowerContract towerContract) {
        if (towerContractService.updateById(towerContract)) {
            return success(towerContract.getContractId());
        }
        return fail("修改失败");
    }

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

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

    @PreAuthorize("hasAuthority('tower:towerContract:update')")
    @OperationLog
    @ApiOperation("批量修改合同存档")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<TowerContract> batchParam) {
        if (batchParam.update(towerContractService, "contract_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

}
