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

import com.neusoft.smart.pos.framework.dto.ResponseData;
import com.neusoft.smart.pos.dto.inventory.request.BatchCreateRetailGoodsRequest;
import com.neusoft.smart.pos.dto.inventory.request.GetRetailGoodsListRequest;
import com.neusoft.smart.pos.dto.inventory.request.PostInventoryGoodsInfoRequest;
import com.neusoft.smart.pos.dto.inventory.request.PutInventoryGoodsInfoRequest;
import com.neusoft.smart.pos.dto.inventory.response.GoodsListResponse;
import com.neusoft.smart.pos.dto.inventory.response.InventoryGoodsResponse;
import com.neusoft.smart.pos.inventory.service.InventoryGoodsInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.util.Pair;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Api(value = "零售商品管理使用rest接口", description = "零售商品管理使用rest接口")
@RequestMapping(path = "/goods/retail")
@Validated
public class InventoryGoodsInfoController {
    
    @Autowired
    private InventoryGoodsInfoService inventoryGoodsInfoService;
    
    @ApiOperation(value = "后台零售商品查询列表接口")
    @GetMapping("/goods")
    public ResponseData<GoodsListResponse<InventoryGoodsResponse>> getGoodsList(@Validated @ModelAttribute GetRetailGoodsListRequest request) {
        return new ResponseData<>(inventoryGoodsInfoService.selectGoodsList(request));
    }
    
    @ApiOperation(value = "后台零售商品查询接口")
    @GetMapping("/goods/{id}")
    public ResponseData<InventoryGoodsResponse> getGoods(@PathVariable Integer id) {
        return new ResponseData<>(inventoryGoodsInfoService.selectGoodsById(id));
    }
    @ApiOperation(value = "pos通过一维码查询零售商品接口", notes = "pos通过一维码查询零售商品接口")
    @GetMapping("/goods/{orgId}/{shopId}/{barCode}")
    public ResponseData<InventoryGoodsResponse> getGoodsByBarCode(@PathVariable Integer orgId, @PathVariable Integer shopId, @PathVariable String barCode) {
        return new ResponseData<>(inventoryGoodsInfoService.selectGoodsByBarCode(orgId, shopId, barCode));
    }
    @ApiOperation(value = "pos查询零售商品列表接口", notes = "pos通过门店id查询")
    @GetMapping("/goods/pos/{snCode}")
    public ResponseData<List<InventoryGoodsResponse>> getGoodsListBySnCode(@PathVariable String snCode) {
        return new ResponseData<>(inventoryGoodsInfoService.selectGoodsListBySnCode(snCode));
    }
    
    @ApiOperation(value = "创建零售商品接口")
    @PostMapping("/goods")
    public ResponseData<?> createGoods(@RequestBody PostInventoryGoodsInfoRequest request) {
        inventoryGoodsInfoService.insertGoods(request);
        return new ResponseData<>();
    }
    
    @ApiOperation(value = "修改零售商品接口")
    @PatchMapping("/goods")
    public ResponseData<?> updateGoods(@RequestBody PutInventoryGoodsInfoRequest request) {
        inventoryGoodsInfoService.updateGoods(request);
        return new ResponseData<>();
    }
    
    @ApiOperation(value = "删除零售商品接口")
    @DeleteMapping("/goods/{id}")
    public ResponseData<Integer> deleteGoods(@PathVariable Integer id) {
        inventoryGoodsInfoService.deleteGoods(id);
        return new ResponseData<>(1);
    }
    
    @ApiOperation(value = "解析导入零售csv文件接口", notes = "解析导入零售csv文件接口")
    @PostMapping("/goods/parse/{orgId}/{shopId}")
    public ResponseData<Pair<List<InventoryGoodsResponse>, List<InventoryGoodsResponse>>> importGoodsFile(@PathVariable Integer orgId, @PathVariable Integer shopId, @RequestParam("file") MultipartFile file) {
        return new ResponseData<>(inventoryGoodsInfoService.parseGoodsListFromCsv(orgId, shopId, file));
    }
    
    @ApiOperation(value = "零售商品批量添加接口", notes = "零售商品批量添加接口")
    @PostMapping("/goods/batch")
    public ResponseData<Integer> batchInsert(@RequestBody BatchCreateRetailGoodsRequest request) {
        inventoryGoodsInfoService.batchInsertGoodsList(request);
        return new ResponseData<>(1);
    }
    
    @ApiOperation(value = "下载导入模板接口")
    @GetMapping("/downloadTemplate/{shopId}")
    public void downloadTemplate(@PathVariable(value = "shopId") Integer shopId, HttpServletResponse response) throws Exception {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + new String(("导入商品模板.xlsx").getBytes(), "iso-8859-1"));
        inventoryGoodsInfoService.downloadTemplate(shopId, response.getOutputStream());
    }
}
