package com.gxwebsoft.tower.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.service.TowerSettleDetailService;
import com.gxwebsoft.tower.service.TowerSettleService;
import com.gxwebsoft.tower.param.TowerSettleParam;
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.ArrayList;
import java.util.List;

/**
 * 结算管理控制器
 *
 * @author 科技小王子
 * @since 2023-06-11 19:19:10
 */
@Api(tags = "结算管理管理")
@RestController
@RequestMapping("/api/tower/tower-settle")
public class TowerSettleController extends BaseController {
    @Resource
    private TowerSettleService towerSettleService;
    private TowerSettleDetailService towerSettleDetailService;

    @PreAuthorize("hasAuthority('tower:towerSettle:list')")
    @OperationLog
    @ApiOperation("分页查询结算管理")
    @GetMapping("/page")
    public ApiResult<PageResult<TowerSettle>> page(TowerSettleParam param) {
        MPJLambdaWrapper<TowerSettle> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(TowerSettle.class)
                .select(TowerProject::getProjectName)
                .select(TowerContract::getContactNumber)
                .leftJoin(TowerContract.class, TowerContract::getContractId, TowerSettle::getContractId)
                .leftJoin(TowerProject.class, TowerProject::getProjectId, TowerContract::getProjectId);

        PageParam<TowerSettle, TowerSettleParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerSettleService.page(page, wrapper));
        // 使用关联查询
        //return success(towerSettleService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerSettle:list')")
    @OperationLog
    @ApiOperation("查询全部结算管理")
    @GetMapping()
    public ApiResult<List<TowerSettle>> list(TowerSettleParam param) {
        PageParam<TowerSettle, TowerSettleParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerSettleService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(towerSettleService.listRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerSettle:list')")
    @OperationLog
    @ApiOperation("根据id查询结算管理")
    @GetMapping("/{id}")
    public ApiResult<TowerSettle> get(@PathVariable("id") Integer id) {
        return success(towerSettleService.getById(id));
        // 使用关联查询
        //return success(towerSettleService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('tower:towerSettle:save')")
    @OperationLog
    @ApiOperation("添加结算管理")
    @PostMapping()
    public ApiResult<?> save(@RequestBody TowerSettle towerSettle) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
            towerSettle.setUserId(loginUser.getUserId());
        }
        if (towerSettleService.save(towerSettle)) {
            return success(towerSettle.getSettleId());
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerSettle:update')")
    @OperationLog
    @ApiOperation("修改结算管理")
    @PutMapping()
    public ApiResult<?> update(@RequestBody TowerSettle towerSettle) {
        if (towerSettleService.updateById(towerSettle)) {
            return success(towerSettle.getSettleId());
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('tower:towerSettle:remove')")
    @OperationLog
    @ApiOperation("删除结算管理")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (towerSettleService.removeById(id)) {
            QueryWrapper<TowerSettleDetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("settle_id", id);
            towerSettleDetailService.remove(queryWrapper);
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('tower:towerSettle:save')")
    @OperationLog
    @ApiOperation("批量添加结算管理")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<TowerSettle> list) {
        if (towerSettleService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerSettle:update')")
    @OperationLog
    @ApiOperation("批量修改结算管理")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<TowerSettle> batchParam) {
        if (batchParam.update(towerSettleService, "settle_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('tower:towerSettle:remove')")
    @OperationLog
    @ApiOperation("批量删除结算管理")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (towerSettleService.removeByIds(ids)) {
            QueryWrapper<TowerSettleDetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("settle_id", ids);
            towerSettleDetailService.remove(queryWrapper);
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
