package com.gxwebsoft.tower.controller;

import com.alibaba.fastjson.JSON;
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.service.TowerContractFileService;
import com.gxwebsoft.tower.entity.TowerContractFile;
import com.gxwebsoft.tower.param.TowerContractFileParam;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 合同附件控制器
 *
 * @author 科技小王子
 * @since 2023-06-09 13:42:20
 */
@Api(tags = "合同附件管理")
@RestController
@RequestMapping("/api/tower/tower-contract-file")
public class TowerContractFileController extends BaseController {
    @Resource
    private TowerContractFileService towerContractFileService;

    @OperationLog
    @ApiOperation("分页查询合同附件")
    @GetMapping("/page")
    public ApiResult<PageResult<TowerContractFile>> page(TowerContractFileParam param) {
        PageParam<TowerContractFile, TowerContractFileParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerContractFileService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(towerContractFileService.pageRel(param));
    }

    @OperationLog
    @PreAuthorize("permitAll()")
    @ApiOperation("查询全部合同附件")
    @GetMapping()
    public ApiResult<List<TowerContractFile>> list(TowerContractFileParam param) {
        PageParam<TowerContractFile, TowerContractFileParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerContractFileService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(towerContractFileService.listRel(param));
    }

    @OperationLog
    @ApiOperation("根据id查询合同附件")
    @GetMapping("/{id}")
    public ApiResult<TowerContractFile> get(@PathVariable("id") Integer id) {
        return success(towerContractFileService.getById(id));
        // 使用关联查询
        //return success(towerContractFileService.getByIdRel(id));
    }

    @OperationLog
    @ApiOperation("添加合同附件")
    @PostMapping()
    public ApiResult<?> save(@RequestBody TowerContractFile towerContractFile) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
            towerContractFile.setUserId(loginUser.getUserId());
        }
        if (towerContractFileService.save(towerContractFile)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @OperationLog
    @ApiOperation("修改合同附件")
    @PutMapping()
    public ApiResult<?> update(@RequestBody TowerContractFile towerContractFile) {
        if (towerContractFileService.updateById(towerContractFile)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @OperationLog
    @ApiOperation("删除合同附件")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (towerContractFileService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @OperationLog
    @PreAuthorize("permitAll()")
    @ApiOperation("批量添加合同附件")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<TowerContractFile> list) {
        QueryWrapper<TowerContractFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("contract_id", list.get(0).getContractId());
        towerContractFileService.remove(queryWrapper);
        if (towerContractFileService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @OperationLog
    @ApiOperation("批量修改合同附件")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<TowerContractFile> batchParam) {
        if (batchParam.update(towerContractFileService, "contract_file_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @OperationLog
    @ApiOperation("批量删除合同附件")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (towerContractFileService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
