package cn.iocoder.yudao.module.system.controller.admin.dishes;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.system.controller.admin.dishes.vo.DishesPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dishes.vo.DishesRespVO;
import cn.iocoder.yudao.module.system.controller.admin.dishes.vo.DishesSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.carteen.CarteenDO;
import cn.iocoder.yudao.module.system.dal.dataobject.dishes.DishesDO;
import cn.iocoder.yudao.module.system.dal.dataobject.dishestype.DishesTypeDO;
import cn.iocoder.yudao.module.system.service.carteen.CarteenService;
import cn.iocoder.yudao.module.system.service.dishes.DishesService;
import cn.iocoder.yudao.module.system.service.dishestype.DishesTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
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 javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


@Tag(name = "管理后台 - 菜品管理")
@RestController
@RequestMapping("/t/dishes")
@Validated
public class DishesController {

    @Resource
    private DishesService dishesService;
    @Resource
    private DishesTypeService dishesTypeService;
    @Resource
    private CarteenService carteenService;

    @PostMapping("/create")
    @Operation(summary = "创建菜品管理")
    @PreAuthorize("@ss.hasPermission('t:dishes:create')")
    public CommonResult<Long> createDishes(@Valid @RequestBody DishesSaveReqVO createReqVO) {
        return success(dishesService.createDishes(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新菜品管理")
    @PreAuthorize("@ss.hasPermission('t:dishes:update')")
    public CommonResult<Boolean> updateDishes(@Valid @RequestBody DishesSaveReqVO updateReqVO) {
        dishesService.updateDishes(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除菜品管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('t:dishes:delete')")
    public CommonResult<Boolean> deleteDishes(@RequestParam("id") Long id) {
        dishesService.deleteDishes(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得菜品管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('t:dishes:query')")
    public CommonResult<DishesRespVO> getDishes(@RequestParam("id") Long id) {
        return success(dishesService.getDishes1(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得菜品管理分页")
    @PreAuthorize("@ss.hasPermission('t:dishes:query')")
    public CommonResult<PageResult<DishesRespVO>> getDishesPage(@Valid DishesPageReqVO pageReqVO) {
        PageResult<DishesDO> pageResult = dishesService.getDishesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DishesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出菜品管理 Excel")
    @PreAuthorize("@ss.hasPermission('t:dishes:export')")
    @OperateLog(type = EXPORT)
    public void exportDishesExcel(@Valid DishesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DishesDO> list = dishesService.getDishesPage(pageReqVO).getList();
        List<DishesRespVO> respVOList = BeanUtils.toBean(list, DishesRespVO.class);
        //转换类型，门店
        List<CarteenDO> carteenList = carteenService.getCarteenList();
        Map<Long, String> carteenMap = carteenList.stream().collect(Collectors.toMap(CarteenDO::getId, CarteenDO::getStoresName));
        List<DishesTypeDO> dishesTypeList = dishesTypeService.getList();
        Map<Long, String> dishesTypeMap = dishesTypeList.stream().collect(Collectors.toMap(DishesTypeDO::getId, DishesTypeDO::getDishesTypeName));
        for(DishesRespVO vo : respVOList){
            vo.setCarteenName(carteenMap.get(vo.getCarteenId()));
            vo.setDishecTypeName(dishesTypeMap.get(Long.valueOf(vo.getDishecType())));
        }
        // 导出 Excel
        ExcelUtils.write(response, "菜品管理.xls", "数据", DishesRespVO.class,respVOList);
    }

}