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.TowerContractFile;
import com.gxwebsoft.tower.entity.TowerEquipment;
import com.gxwebsoft.tower.service.TowerContractEquipmentService;
import com.gxwebsoft.tower.entity.TowerContractEquipment;
import com.gxwebsoft.tower.param.TowerContractEquipmentParam;
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.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 合同设备清单控制器
 *
 * @author 科技小王子
 * @since 2023-06-09 13:42:20
 */
@Api(tags = "合同设备清单管理")
@RestController
@RequestMapping("/api/tower/tower-contract-equipment")
public class TowerContractEquipmentController extends BaseController {
    @Resource
    private TowerContractEquipmentService towerContractEquipmentService;

    @OperationLog
    @ApiOperation("分页查询合同设备清单")
    @GetMapping("/page")
    public ApiResult<PageResult<TowerContractEquipment>> page(TowerContractEquipmentParam param) {
        PageParam<TowerContractEquipment, TowerContractEquipmentParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerContractEquipmentService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(towerContractEquipmentService.pageRel(param));
    }

    @OperationLog
    @ApiOperation("查询全部合同设备清单")
    @GetMapping()
    public ApiResult<List<TowerContractEquipment>> list(@RequestParam Map<String, String> params) {
        int contractId = Integer.parseInt(params.get("contractId"));

        MPJLambdaWrapper<TowerContractEquipment> wrapper = new MPJLambdaWrapper<>();
        wrapper.eq(TowerContractEquipment::getContractId, contractId)
        .selectAll(TowerContractEquipment.class)
                .selectAs(TowerEquipment::getName, TowerEquipment::getEquipmentName)
                .select(TowerEquipment::getFactoryNo)
                .select(TowerEquipment::getEquipmentNo)
                .select(TowerEquipment::getFilingNo)
                .leftJoin(TowerEquipment.class, TowerEquipment::getEquipmentId, TowerContractEquipment::getEquipmentId);
        TowerContractEquipmentParam towerContractEquipmentParam = new TowerContractEquipmentParam();
//        towerContractEquipmentParam.setContractId(contractId);

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

    @OperationLog
    @ApiOperation("根据id查询合同设备清单")
    @GetMapping("/{id}")
    public ApiResult<TowerContractEquipment> get(@PathVariable("id") Integer id) {
        return success(towerContractEquipmentService.getById(id));
        // 使用关联查询
        //return success(towerContractEquipmentService.getByIdRel(id));
    }

    @OperationLog
    @ApiOperation("添加合同设备清单")
    @PostMapping()
    public ApiResult<?> save(@RequestBody TowerContractEquipment towerContractEquipment) {
        // 记录当前登录用户idfc vbnm,dxcybc yhguc
        User loginUser = getLoginUser();
        if (loginUser != null) {
            towerContractEquipment.setUserId(loginUser.getUserId());
        }
        if (towerContractEquipmentService.save(towerContractEquipment)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @OperationLog
    @ApiOperation("修改合同设备清单")
    @PutMapping()
    public ApiResult<?> update(@RequestBody TowerContractEquipment towerContractEquipment) {
        if (towerContractEquipmentService.updateById(towerContractEquipment)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

    @OperationLog
    @ApiOperation("批量添加合同设备清单")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<TowerContractEquipment> list) {
        QueryWrapper<TowerContractEquipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("contract_id", list.get(0).getContractId());
        towerContractEquipmentService.remove(queryWrapper);
        if (towerContractEquipmentService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @OperationLog
    @ApiOperation("批量修改合同设备清单")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<TowerContractEquipment> batchParam) {
        if (batchParam.update(towerContractEquipmentService, "contract_equipment_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

}
