package com.gxwebsoft.tower.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.tower.entity.TowerContractSettleRule;
import com.gxwebsoft.tower.service.TowerContractSettleEquipmentService;
import com.gxwebsoft.tower.entity.TowerContractSettleEquipment;
import com.gxwebsoft.tower.param.TowerContractSettleEquipmentParam;
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;
import java.util.Map;
import java.util.Objects;

/**
 * 合同结算设备控制器
 *
 * @author 科技小王子
 * @since 2023-06-10 16:52:12
 */
@Api(tags = "合同结算设备管理")
@RestController
@RequestMapping("/api/tower/tower-contract-settle-equipment")
public class TowerContractSettleEquipmentController extends BaseController {
    @Resource
    private TowerContractSettleEquipmentService towerContractSettleEquipmentService;

    @PreAuthorize("hasAuthority('tower:towerContract:list')")
    @OperationLog
    @ApiOperation("分页查询合同结算设备")
    @GetMapping("/page")
    public ApiResult<PageResult<TowerContractSettleEquipment>> page(TowerContractSettleEquipmentParam param) {
        PageParam<TowerContractSettleEquipment, TowerContractSettleEquipmentParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerContractSettleEquipmentService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(towerContractSettleEquipmentService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerContract:list')")
    @OperationLog
    @ApiOperation("查询全部合同结算设备")
    @GetMapping()
    public ApiResult<List<TowerContractSettleEquipment>> list(@RequestParam Map<String, String> param) {
        int contractId = Integer.parseInt(param.get("contractId"));
        TowerContractSettleEquipmentParam towerContractSettleEquipmentParam = new TowerContractSettleEquipmentParam();
        towerContractSettleEquipmentParam.setContractId(contractId);
        PageParam<TowerContractSettleEquipment, TowerContractSettleEquipmentParam> page = new PageParam<>(towerContractSettleEquipmentParam);
        page.setDefaultOrder("create_time desc");
        return success(towerContractSettleEquipmentService.list(page.getOrderWrapper()));
        // 使用关联查询
//        return success(null);
    }

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

    @PreAuthorize("hasAuthority('tower:towerContract:save')")
    @OperationLog
    @ApiOperation("添加合同结算设备")
    @PostMapping()
    public ApiResult<?> save(@RequestBody TowerContractSettleEquipment towerContractSettleEquipment) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          towerContractSettleEquipment.setUserId(loginUser.getUserId());
        }
        if (towerContractSettleEquipmentService.save(towerContractSettleEquipment)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerContract:update')")
    @OperationLog
    @ApiOperation("修改合同结算设备")
    @PutMapping()
    public ApiResult<?> update(@RequestBody TowerContractSettleEquipment towerContractSettleEquipment) {
        if (towerContractSettleEquipmentService.updateById(towerContractSettleEquipment)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

    @PreAuthorize("hasAuthority('tower:towerContract:save')")
    @OperationLog
    @ApiOperation("批量添加合同结算设备")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<TowerContractSettleEquipment> list) {
        QueryWrapper<TowerContractSettleEquipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("contract_id", list.get(0).getContractId());
        towerContractSettleEquipmentService.remove(queryWrapper);
        if (towerContractSettleEquipmentService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerContract:update')")
    @OperationLog
    @ApiOperation("批量修改合同结算设备")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<TowerContractSettleEquipment> batchParam) {
        if (batchParam.update(towerContractSettleEquipmentService, "contract_settle_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

}
