package com.muyu.system.controller;

import com.muyu.common.core.constant.Constants;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.web.page.TableDataInfo;
import com.muyu.common.system.domain.SysEnt;
import com.muyu.system.domain.req.EntAddReq;
import com.muyu.system.domain.req.EntListReq;
import com.muyu.system.domain.req.EntUpdateReq;
import com.muyu.system.domain.resp.EntResp;
import com.muyu.system.service.SysEntService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 企业管理控制层
 * @Author: chenruijia
 * @Date 2024/9/26 0:21
 * @Description 企业管理控制层
 */
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/ent")
@Tag(name = "SysEntController",description = "企业管理")
public class SysEntController extends BaseController {

    /**
     * 企业管理业务层
     */
    private final SysEntService sysEntService;

    /**
     * 获取企业信息
     * @return 响应结果
     */
    @GetMapping(value = "/getInfo")
    @Operation(summary = "获取企业信息", description = "现有的基础企业信息")
    public Result<List<EntResp>> getInfo() {
        return Result.success(
                sysEntService.selectList(new EntListReq()), Constants.SUCCESS_MESSAGE
        );
    }

    /**
     * 查询企业信息列表
     * @param entListReq 企业列表请求对象
     * @return 响应结果
     */
    @PostMapping(value = "/selectList")
    @Operation(summary = "查询企业信息列表", description = "分页展示企业信息")
    public Result<TableDataInfo<EntResp>> selectList(@RequestBody EntListReq entListReq) {
        startPage();
        List<EntResp> entRespList = sysEntService.selectList(entListReq);
        return getDataTable(entRespList);
    }

    /**
     * 根据ID获取企业信息
     * @param entId 企业ID
     * @return 响应结果
     */
    @GetMapping(value = "/{entId}")
    @Operation(summary = "根据ID获取企业信息", description = "根据企业唯一标识获取单体企业信息")
    public Result<EntResp> getById(
            @Schema(title = "企业ID", type = "Long", defaultValue = "1", description = "企业唯一标识")
            @PathVariable(value = "entId") Long entId) {
        return Result.success(EntResp.entBuild(sysEntService.getById(entId)), Constants.SUCCESS_MESSAGE);
    }

    /**
     * 添加企业信息
     * @param entAddReq 企业添加请求对象
     * @return 响应结果
     */
    @PostMapping
    @Operation(summary = "添加企业信息", description = "将企业信息添加到系统中")
    public Result<String> save(@Validated @RequestBody EntAddReq entAddReq) {
        sysEntService.save(EntAddReq.addBuild(entAddReq));
        return Result.success(null, Constants.SUCCESS_MESSAGE);
    }

    /**
     * 修改企业信息
     * @param entId 企业ID
     * @param entUpdateReq 企业修改请求对象
     * @return 响应结果
     */
    @PutMapping(value = "/{entId}")
    @Operation(summary = "修改企业信息",description = "根据企业唯一标识修改企业信息")
    public Result<String> update(@Schema(title = "企业ID",type = "Long",defaultValue = "1",description = "企业唯一标识")
                                     @NotNull(message = "企业ID不能为空") @PathVariable(value = "entId") Long entId,
            @RequestBody EntUpdateReq entUpdateReq){
        sysEntService.updateById(EntUpdateReq.updateBuild(entUpdateReq,()->entId));
        return Result.success(null,Constants.SUCCESS_MESSAGE);
    }

    /**
     * 删除企业信息
     * @param entId 企业ID
     * @return
     */
    @RequestMapping(value = "/entId",method = RequestMethod.DELETE)
    @Operation(summary = "删除企业信息",description = "根据企业唯一标识删除企业记录")
    public Result<String> remove(@Schema(title = "企业ID",type = "Long",defaultValue = "1",description = "企业唯一标识")
                                 @NotNull(message = "企业ID不能为空") @PathVariable(value = "entId")Long entId){
        sysEntService.removeById(entId);
        return Result.success(null,Constants.SUCCESS_MESSAGE);
    }
}
