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.User;
import com.gxwebsoft.tower.entity.TowerLendingSettleList;
import com.gxwebsoft.tower.entity.TowerSettle;
import com.gxwebsoft.tower.service.TowerInvoicingSettleService;
import com.gxwebsoft.tower.entity.TowerInvoicingSettle;
import com.gxwebsoft.tower.param.TowerInvoicingSettleParam;
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;

/**
 * 开票结算单分摊控制器
 *
 * @author 科技小王子
 * @since 2023-06-17 12:47:08
 */
@Api(tags = "开票结算单分摊管理")
@RestController
@RequestMapping("/api/tower/tower-invoicing-settle")
public class TowerInvoicingSettleController extends BaseController {
    @Resource
    private TowerInvoicingSettleService towerInvoicingSettleService;

    @PreAuthorize("hasAuthority('tower:towerInvoicing:list')")
    @OperationLog
    @ApiOperation("分页查询开票结算单分摊")
    @GetMapping("/page")
    public ApiResult<PageResult<TowerInvoicingSettle>> page(TowerInvoicingSettleParam param) {
        MPJLambdaWrapper<TowerInvoicingSettle> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(TowerInvoicingSettle.class)
                .select(TowerSettle::getSettleNo)
                .select(TowerSettle::getStartDate)
                .select(TowerSettle::getTotalAmount)
                .leftJoin(TowerSettle.class, TowerSettle::getSettleId, TowerInvoicingSettle::getSettleId);
        PageParam<TowerInvoicingSettle, TowerInvoicingSettleParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerInvoicingSettleService.page(page, wrapper));
        // 使用关联查询
        //return success(towerInvoicingSettleService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerInvoicing:list')")
    @OperationLog
    @ApiOperation("查询全部开票结算单分摊")
    @GetMapping()
    public ApiResult<List<TowerInvoicingSettle>> list(TowerInvoicingSettleParam param) {
        PageParam<TowerInvoicingSettle, TowerInvoicingSettleParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerInvoicingSettleService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(towerInvoicingSettleService.listRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerInvoicing:list')")
    @OperationLog
    @ApiOperation("根据id查询开票结算单分摊")
    @GetMapping("/{id}")
    public ApiResult<TowerInvoicingSettle> get(@PathVariable("id") Integer id) {
        return success(towerInvoicingSettleService.getById(id));
        // 使用关联查询
        //return success(towerInvoicingSettleService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('tower:towerInvoicing:save')")
    @OperationLog
    @ApiOperation("添加开票结算单分摊")
    @PostMapping()
    public ApiResult<?> save(@RequestBody TowerInvoicingSettle towerInvoicingSettle) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          towerInvoicingSettle.setUserId(loginUser.getUserId());
        }
        if (towerInvoicingSettleService.save(towerInvoicingSettle)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerInvoicing:update')")
    @OperationLog
    @ApiOperation("修改开票结算单分摊")
    @PutMapping()
    public ApiResult<?> update(@RequestBody TowerInvoicingSettle towerInvoicingSettle) {
        if (towerInvoicingSettleService.updateById(towerInvoicingSettle)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('tower:towerInvoicing:remove')")
    @OperationLog
    @ApiOperation("删除开票结算单分摊")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (towerInvoicingSettleService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('tower:towerInvoicing:save')")
    @OperationLog
    @ApiOperation("批量添加开票结算单分摊")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<TowerInvoicingSettle> list) {
        QueryWrapper<TowerInvoicingSettle> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("invoicing_id", list.get(0).getInvoicingId());
        towerInvoicingSettleService.remove(queryWrapper);
        if (towerInvoicingSettleService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerInvoicing:update')")
    @OperationLog
    @ApiOperation("批量修改开票结算单分摊")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<TowerInvoicingSettle> batchParam) {
        if (batchParam.update(towerInvoicingSettleService, "invoicing_settle_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('tower:towerInvoicing:remove')")
    @OperationLog
    @ApiOperation("批量删除开票结算单分摊")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (towerInvoicingSettleService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
