package com.jeesharp.business.modules.food.controller;

import com.jeesharp.business.modules.food.application.FoodCompanyEmployeeApplicationService;
import com.jeesharp.business.modules.food.dto.FoodCompanySaveDTO;
import com.jeesharp.business.modules.food.dto.FoodStatusUpdateDTO;
import com.jeesharp.business.modules.food.entity.FoodCompany;
import com.jeesharp.business.modules.food.vo.FoodCompanyInfoVO;
import com.jeesharp.business.modules.food.vo.FoodCompanyListVO;
import com.jeesharp.business.modules.food.vo.FoodCompanyQueryVO;
import com.jeesharp.business.modules.food.vo.FoodCompanySearchVO;
import com.jeesharp.framework.pojo.AjaxResult;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.pojo.query.PageRespVO;
import com.jeesharp.framework.web.controller.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/food/company/v1")
@Tag(name = "订餐企业管理")
@AllArgsConstructor
public class FoodCompanyController extends BaseController {

    private final FoodCompanyEmployeeApplicationService foodCompanyEmployeeApplicationService;

    @Operation(summary = "获取列表（分页）")
    @PreAuthorize("@ss.hasPermission('manager:business:food:company:list')")
    @PostMapping("list")
    public AjaxResult<PageRespVO<FoodCompanyListVO>> list(@RequestBody FoodCompanyQueryVO query) {
        return AjaxResult.okData(foodCompanyEmployeeApplicationService.findCompanyPage(new Page<FoodCompany>(query), query));
    }

    @Operation(summary = "获取详情")
    @PreAuthorize("@ss.hasPermission('manager:business:food:company:info')")
    @GetMapping("info")
    public AjaxResult<FoodCompanyInfoVO> info(@RequestParam("id") String id) {
        return AjaxResult.okData(foodCompanyEmployeeApplicationService.getCompany(id));
    }

    @Operation(summary = "搜索启用企业")
    @PreAuthorize("@ss.hasPermission('manager:business:food:company:search')")
    @GetMapping("search")
    public AjaxResult<List<FoodCompanySearchVO>> search(@RequestParam(value = "keyword", required = false) String keyword) {
        return AjaxResult.okData(foodCompanyEmployeeApplicationService.searchCompanies(keyword));
    }

    @Operation(summary = "保存")
    @PreAuthorize("@ss.hasPermission('manager:business:food:company:save')")
    @PostMapping("save")
    public AjaxResult<Void> save(@RequestBody @Valid FoodCompanySaveDTO dto) {
        foodCompanyEmployeeApplicationService.saveCompany(dto);
        return AjaxResult.ok("保存成功");
    }

    @Operation(summary = "更新状态")
    @PreAuthorize("@ss.hasPermission('manager:business:food:company:status')")
    @PostMapping("status")
    public AjaxResult<Void> status(@RequestBody @Valid FoodStatusUpdateDTO dto) {
        foodCompanyEmployeeApplicationService.updateCompanyStatus(dto.getId(), dto.getStatus());
        return AjaxResult.ok("更新成功");
    }

    @Operation(summary = "删除")
    @PreAuthorize("@ss.hasPermission('manager:business:food:company:delete')")
    @GetMapping("delete")
    public AjaxResult<Void> delete(@RequestParam("ids") String ids) {
        foodCompanyEmployeeApplicationService.deleteCompany(ids);
        return AjaxResult.ok("删除成功");
    }
}
