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

import com.neusoft.smart.pos.framework.dto.ResponseData;
import com.neusoft.smart.pos.framework.exception.BusinessCommonException;
import com.neusoft.smart.pos.inventory.constants.GoodsConstant;
import com.neusoft.smart.pos.inventory.constants.GoodsErrorCode;
import com.neusoft.smart.pos.dto.inventory.request.CreateTagRequest;
import com.neusoft.smart.pos.dto.inventory.request.GetTagsRequest;
import com.neusoft.smart.pos.dto.inventory.request.ReorderRequest;
import com.neusoft.smart.pos.dto.inventory.request.UpdateTagRequest;
import com.neusoft.smart.pos.dto.inventory.response.TagResponse;
import com.neusoft.smart.pos.inventory.service.TagService;
import com.neusoft.smart.pos.redis.bean.DeviceInfo;
import com.neusoft.smart.pos.utils.PosDeviceRedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
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.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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@Api(value = "商品管理标签rest接口", description = "商品管理标签rest接口")
@RequestMapping(path = "/goods/tag")
@Validated
public class InventoryGoodsTagController {
    
    @Autowired
    TagService tagService;
    
    @ApiOperation(value = "标签列表接口")
    @GetMapping("")
    public ResponseData<List<TagResponse>> getTags(GetTagsRequest request) {
        return new ResponseData<>(tagService.selectTags(request));
    }
    
    
    @ApiOperation(value = "标签列表接口")
    @GetMapping("/flavor/pos/{snCode}")
    public ResponseData<List<String>> getFlavorTagsByPos(@PathVariable String snCode) {
        DeviceInfo deviceInfo = PosDeviceRedisUtils.getDeviceInfoBySerialNo(snCode);
        
        if (null == deviceInfo) {
            throw new BusinessCommonException(GoodsErrorCode.DEVICE_ORG_EMPTY);
        }
        
        GetTagsRequest request = new GetTagsRequest();
        request.setIndustry(GoodsConstant.Industry.Catering.getCode());
        request.setType(GoodsConstant.FLAVOR_TAG);
        request.setOrg(deviceInfo.getOrganizationId());
        request.setShop(deviceInfo.getStoreId());
        
        return new ResponseData<>(tagService.selectTags(request).stream().map(response -> response.getName()).collect(Collectors.toList()));
    }
    
    @ApiOperation(value = "创建标签接口")
    @PostMapping("")
    public ResponseData<TagResponse> createTag(@RequestBody CreateTagRequest request) {
        return new ResponseData<>(tagService.createTag(request));
    }
    
    @ApiOperation(value = "更行标签接口")
    @PutMapping("/{id}")
    public ResponseData<Integer> updateTag(@PathVariable Integer id, @RequestBody UpdateTagRequest request) {
        tagService.updateTag(id, request);
        return new ResponseData<>(1);
    }
    
    @ApiOperation(value = "标签重排接口")
    @PostMapping("/re-order")
    public ResponseData<Integer> reorder(@RequestBody List<ReorderRequest> request) {
        Map<Integer, Integer> orderMap = new HashMap<>();
        
        for (ReorderRequest item : request) {
            orderMap.put(item.getId(), item.getOrder());
        }
        
        tagService.reorder(orderMap);
        return new ResponseData<>(1);
    }
    
    @ApiOperation(value = "餐饮标签删除接口")
    @DeleteMapping("/{id}")
    public ResponseData<Integer> deleteCateringCategory(@PathVariable Integer id) {
        tagService.deleteTag(id);
        return new ResponseData<>(1);
    }
}
