package com.gxwebsoft.tower.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJLambdaQueryWrapper;
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.*;
import com.gxwebsoft.tower.service.TowerIncomeEquipmentService;
import com.gxwebsoft.tower.param.TowerIncomeEquipmentParam;
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 17:06:39
 */
@Api(tags = "收款设备费用分摊管理")
@RestController
@RequestMapping("/api/tower/tower-income-equipment")
public class TowerIncomeEquipmentController extends BaseController {
    @Resource
    private TowerIncomeEquipmentService towerIncomeEquipmentService;

    @PreAuthorize("hasAuthority('tower:towerIncome:list')")
    @OperationLog
    @ApiOperation("分页查询收款设备费用分摊")
    @GetMapping("/page")
    public ApiResult<PageResult<TowerIncomeEquipment>> page(TowerIncomeEquipmentParam param) {
        PageParam<TowerIncomeEquipment, TowerIncomeEquipmentParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerIncomeEquipmentService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(towerIncomeEquipmentService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerIncome:list')")
    @OperationLog
    @ApiOperation("查询全部收款设备费用分摊")
    @GetMapping()
    public ApiResult<List<TowerIncomeEquipment>> list(TowerIncomeEquipmentParam param) {
        MPJLambdaWrapper<TowerIncomeEquipment> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(TowerIncomeEquipment.class)
                .select(TowerEquipment::getEquipmentName)
                .selectAs(TowerEquipment::getModel, TowerEquipment::getEquipmentModel)
                .select(TowerEquipment::getFilingNo)
                .select(TowerEquipment::getFactoryNo)
                .select(TowerEquipment::getEquipmentNo)
                .select(TowerContractEquipment::getRentAmount)
                .leftJoin(TowerContractEquipment.class, TowerContractEquipment::getEquipmentId, TowerIncomeEquipment::getEquipmentId)
                .leftJoin(TowerEquipment.class, TowerEquipment::getEquipmentId, TowerContractEquipment::getEquipmentId);

        PageParam<TowerIncomeEquipment, TowerIncomeEquipmentParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerIncomeEquipmentService.list(wrapper));
        // 使用关联查询
        //return success(towerIncomeEquipmentService.listRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerIncome:list')")
    @OperationLog
    @ApiOperation("根据id查询收款设备费用分摊")
    @GetMapping("/{id}")
    public ApiResult<TowerIncomeEquipment> get(@PathVariable("id") Integer id) {
        return success(towerIncomeEquipmentService.getById(id));
        // 使用关联查询
        //return success(towerIncomeEquipmentService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('tower:towerIncome:save')")
    @OperationLog
    @ApiOperation("添加收款设备费用分摊")
    @PostMapping()
    public ApiResult<?> save(@RequestBody TowerIncomeEquipment towerIncomeEquipment) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
            towerIncomeEquipment.setUserId(loginUser.getUserId());
        }
        if (towerIncomeEquipmentService.save(towerIncomeEquipment)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerIncome:update')")
    @OperationLog
    @ApiOperation("修改收款设备费用分摊")
    @PutMapping()
    public ApiResult<?> update(@RequestBody TowerIncomeEquipment towerIncomeEquipment) {
        if (towerIncomeEquipmentService.updateById(towerIncomeEquipment)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('tower:towerIncome:remove')")
    @OperationLog
    @ApiOperation("删除收款设备费用分摊")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (towerIncomeEquipmentService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('tower:towerIncome:save')")
    @OperationLog
    @ApiOperation("批量添加收款设备费用分摊")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<TowerIncomeEquipment> list) {
        if (towerIncomeEquipmentService.saveBatch(list)) {
            QueryWrapper<TowerIncomeEquipment> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("income_id", list.get(0).getIncomeId());
            towerIncomeEquipmentService.remove(queryWrapper);
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerIncome:update')")
    @OperationLog
    @ApiOperation("批量修改收款设备费用分摊")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<TowerIncomeEquipment> batchParam) {
        if (batchParam.update(towerIncomeEquipmentService, "income_equipment_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('tower:towerIncome:remove')")
    @OperationLog
    @ApiOperation("批量删除收款设备费用分摊")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (towerIncomeEquipmentService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
