package com.cool.bang.modules.business.controller;

import com.cool.bang.common.base.BaseController;
import com.cool.bang.common.constant.ControllerConstant;
import com.cool.bang.common.web.request.PageDomain;
import com.cool.bang.common.web.response.Result;
import com.cool.bang.common.web.response.module.ResultTable;
import com.cool.bang.modules.business.domain.LpbUnit;
import com.cool.bang.modules.business.service.ILpbUnitService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 单元信息
 *
 * @author Sy
 * @version 1.0.0
 * 2024-10-22 09:26
 */
@RestController
@RequestMapping(ControllerConstant.BUSINESS + "unit/v1")
public class LpbUnitController extends BaseController {

    private ILpbUnitService lpbUnitService;

    @Autowired
    public LpbUnitController(ILpbUnitService lpbUnitService) {
        this.lpbUnitService = lpbUnitService;
    }

    @GetMapping
    public ResultTable main(PageDomain pageDomain, LpbUnit param) {
        PageInfo<LpbUnit> lpbUnitPage = lpbUnitService.page(pageDomain, param);
        return pageTable(lpbUnitPage.getList(), lpbUnitPage.getTotal());
    }

    @GetMapping("{id}")
    public Result get(@PathVariable Long id) {
        return success(lpbUnitService.queryById(id));
    }

    @GetMapping("select")
    public Result getSelect(Long buildId) {
        LpbUnit lpbUnit = new LpbUnit();
        lpbUnit.setBuildingId(buildId);
        return success(lpbUnitService.list(lpbUnit));
    }

    @PostMapping
    public Result add(LpbUnit lpbUnit) {
        lpbUnit.insertConfigure();
        return decide(lpbUnitService.save(lpbUnit));
    }

    @PostMapping("edit")
    public Result edit(LpbUnit lpbUnit) {
        lpbUnitService.updateHouse(lpbUnit.getId(),lpbUnit.getUnitName());
        return decide(lpbUnitService.update(lpbUnit));
    }

    @DeleteMapping("{id}")
    public Result remove(@PathVariable Integer id) {
        return decide(lpbUnitService.removeById(id));
    }

}
