package com.neusoft.smart.pos.inventory.controller;

import com.neusoft.smart.pos.framework.bean.PaginationExtendBean;
import com.neusoft.smart.pos.framework.dto.ResponseData;
import com.neusoft.smart.pos.inventory.constants.GoodsConstant;
import com.neusoft.smart.pos.dto.inventory.request.CreateMenuRequest;
import com.neusoft.smart.pos.dto.inventory.request.GetCategoriesRequest;
import com.neusoft.smart.pos.dto.inventory.request.GetTagsRequest;
import com.neusoft.smart.pos.dto.inventory.request.GoodsApprovalQueryRequest;
import com.neusoft.smart.pos.dto.inventory.request.GoodsApprovalRequest;
import com.neusoft.smart.pos.dto.inventory.request.InventoryGoodsQueryRequest;
import com.neusoft.smart.pos.dto.inventory.request.PatchUpdateMenuRequest;
import com.neusoft.smart.pos.dto.inventory.request.PosGetMenusRequest;
import com.neusoft.smart.pos.dto.inventory.request.ReorderRequest;
import com.neusoft.smart.pos.dto.inventory.response.CategoryResponse;
import com.neusoft.smart.pos.dto.inventory.response.FullMenuResponse;
import com.neusoft.smart.pos.dto.inventory.response.GoodsApprovalOpinionResponse;
import com.neusoft.smart.pos.dto.inventory.response.MenuListResponse;
import com.neusoft.smart.pos.dto.inventory.response.PosCategoryMenuResponse;
import com.neusoft.smart.pos.dto.inventory.response.PosMenuResponse;
import com.neusoft.smart.pos.dto.inventory.response.SimpleMenuResponse;
import com.neusoft.smart.pos.dto.inventory.response.TagResponse;
import com.neusoft.smart.pos.inventory.service.CateringMenuService;
import com.neusoft.smart.pos.inventory.service.InventoryGoodsCategoryService;
import com.neusoft.smart.pos.inventory.service.TagService;
import com.neusoft.smart.pos.utils.RequestHeaderUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
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.ModelAttribute;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
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.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Api(value = "菜单管理使用rest接口", description = "菜单管理使用rest接口")
@RequestMapping(path = "/goods/catering")
@Validated
public class GoodsCateringMenuController {
    
    @Autowired
    CateringMenuService menuService;
    
    @Autowired
    InventoryGoodsCategoryService categoryService;
    
    @Autowired
    TagService tagService;
    
    @ApiOperation(value = "后台查询菜单列表接口")
    @GetMapping("/menus")
    public ResponseData<MenuListResponse> getList(@Validated @ModelAttribute InventoryGoodsQueryRequest request) {
        return new ResponseData<>(menuService.selectMenus(request));
    }
    
    
    @ApiOperation(value = "微信查询菜单列表接口")
    @GetMapping("/menus/shop/{shopId}")
    public ResponseData<MenuListResponse> getList(@PathVariable Integer shopId) {
        return new ResponseData<>(menuService.selectMenuByShopId(shopId));
    }
    
    @ApiOperation(value = "查询菜品接口")
    @GetMapping("/menus/{id}")
    public ResponseData<FullMenuResponse> getMenu(@PathVariable Integer id) {
        return new ResponseData<>(menuService.selectMenuById(id));
    }
    
    @ApiOperation(value = "pos查询菜单列表接口", notes = "pos通过门店id查询")
    @GetMapping("/menus/pos")
    public ResponseData<List<PosMenuResponse>> getListBySn(@Validated @ModelAttribute PosGetMenusRequest request) {
        return new ResponseData<>(menuService.selectMenusBySnCode(request.getSnCode()));
    }
    
    @ApiOperation(value = "pos查询菜单列表接口", notes = "pos通过门店id查询")
    @GetMapping("/v2/menus/pos")
    public ResponseData<List<PosCategoryMenuResponse>> getCategoryOrderedMenusListBySn(@Validated @ModelAttribute PosGetMenusRequest request) {
        return new ResponseData<>(menuService.selectCategoryMenusBySnCode(request.getSnCode()));
    }

//    @ApiOperation(value = "pos查询菜单列表接口(gateway)", notes = "pos通过门店id查询(gateway)")
//    @GetMapping("/gateway/v2/menus/pos/{sn}")
//    public ResponseData<List<PosCategoryMenuResponse>> getCategoryOrderedMenusListBySn4Gateway(
//            @PathVariable String sn) {
//        return new ResponseData<>(menuService.selectCategoryMenusBySnCode(sn));
//    }

//    @ApiOperation(value = "后台查询pos菜单列表接口", notes = "后台查询pos菜单列表")
//    @GetMapping("/menu-ids/pos/{snCode}")
//    public ResponseData<List<Integer>> getMenuIdsBySn(@PathVariable String snCode) {
//        return new ResponseData<>(menuService.selectMenuIdsByPos(snCode));
//    }

//    @ApiOperation(value = "创建pos菜单接口", notes = "创建pos菜单")
//    @PostMapping("/menus/pos/{snCode}")
//    public ResponseData<Integer> updatePosMenuList(@PathVariable String snCode, @RequestBody List<Integer> menuIds) {
//        menuService.associateMenus(snCode, menuIds);
//        return new ResponseData<>(1);
//    }
    
    @ApiOperation(value = "创建菜单接口")
    @PostMapping("/menus")
    public ResponseData<SimpleMenuResponse> create(@RequestBody CreateMenuRequest request) {
        //配置了需要审批 创建菜单先申请审批
        if (menuService.needApproval(RequestHeaderUtil.getOrgId(), request.getShop())) {
            menuService.beforeInsertMenu(request);
        } else {
            return new ResponseData<>(menuService.insertMenu(request));
        }
        return new ResponseData<>();
    }
    
    @ApiOperation(value = "修改菜单接口")
    @PatchMapping("/menus")
    public ResponseData<SimpleMenuResponse> update(@RequestBody PatchUpdateMenuRequest request) {
        //配置了需要审批 修改菜单先申请审批
        if (menuService.needApproval(RequestHeaderUtil.getOrgId(), request.getShop())) {
            menuService.beforeUpdateMenu(request);
        } else {
            return new ResponseData<>(menuService.updateMenu(request));
        }
        return new ResponseData<>();
    }
    
    @ApiOperation(value = "删除菜单接口")
    @DeleteMapping("/menus/{id}")
    public ResponseData<Integer> delete(@PathVariable Integer id) {
        menuService.deleteMenu(id);
        return new ResponseData<>(1);
    }
    
    @ApiOperation(value = "菜品类别列表接口")
    @GetMapping("/menus/categories")
    public ResponseData<List<CategoryResponse>> getMenuCategories(GetCategoriesRequest request) {
        return new ResponseData<>(categoryService.selectCategories(request, GoodsConstant.Industry.Catering));
    }
    
    @ApiOperation(value = "微信端获取菜品类别列表接口")
    @GetMapping("/menus/categories/{shopId}")
    public ResponseData<List<CategoryResponse>> getMenuCategories(@PathVariable Integer shopId) {
        return new ResponseData<>(categoryService.selectCategoriesByShopId(shopId));
    }
    
    @ApiOperation(value = "菜品标签列表接口")
    @GetMapping("/menus/tags")
    public ResponseData<List<TagResponse>> getMenuTags(GetTagsRequest request) {
        request.setIndustry(GoodsConstant.Industry.Catering.getCode());
        request.setType(0);
        return new ResponseData<>(tagService.selectTags(request));
    }
    
    @PostMapping("/menus/approvals")
    @ApiOperation(value = "分页查询审批列表接口", notes = "分页查询审批列表接口")
    public ResponseData<PaginationExtendBean<GoodsApprovalOpinionResponse>> fetchList(@RequestBody GoodsApprovalQueryRequest goodsApprovalQueryRequest) {
        PaginationExtendBean<GoodsApprovalOpinionResponse> paginationExtendBean = new PaginationExtendBean<>();
        paginationExtendBean.setRows(menuService.getList(goodsApprovalQueryRequest));
        paginationExtendBean.setTotal(menuService.getCount(goodsApprovalQueryRequest));
        return new ResponseData<>(paginationExtendBean);
    }
    
    @PutMapping("/menus/approval")
    @ApiOperation(value = "菜单审批接口", notes = "菜单审批接口")
    public ResponseData<?> approval(@Validated @RequestBody GoodsApprovalRequest goodsApprovalRequest, BindingResult bindingResult) {
        menuService.approvalMenu(goodsApprovalRequest);
        return new ResponseData();
    }
    
    @ApiOperation(value = "重排接口")
    @PostMapping("/re-order")
    public ResponseData<Integer> reorder(@RequestBody List<ReorderRequest> request) {
        menuService.reorder(request.stream().collect(Collectors.toMap(ReorderRequest::getId, ReorderRequest::getOrder)));
        return new ResponseData<>(1);
    }
}
