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.TowerContractFile;
import com.gxwebsoft.tower.param.TowerContractSettleEquipmentParam;
import com.gxwebsoft.tower.service.TowerContractSettleRuleService;
import com.gxwebsoft.tower.entity.TowerContractSettleRule;
import com.gxwebsoft.tower.param.TowerContractSettleRuleParam;
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;

/**
 * 合同结算规则控制器
 *
 * @author 科技小王子
 * @since 2023-06-11 13:48:42
 */
@Api(tags = "合同结算规则管理")
@RestController
@RequestMapping("/api/tower/tower-contract-settle-rule")
public class TowerContractSettleRuleController extends BaseController {
    @Resource
    private TowerContractSettleRuleService towerContractSettleRuleService;

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

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

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

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

    @PreAuthorize("hasAuthority('tower:towerContract:update')")
    @OperationLog
    @ApiOperation("修改合同结算规则")
    @PutMapping()
    public ApiResult<?> update(@RequestBody TowerContractSettleRule towerContractSettleRule) {
        if (towerContractSettleRuleService.updateById(towerContractSettleRule)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

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

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

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

}
