package com.izouma.dingdong.web.merchant;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.izouma.dingdong.repo.merchant.GoodsRepo;
import com.izouma.dingdong.web.BaseController;
import com.izouma.dingdong.domain.merchant.GoodsSpecification;
import com.izouma.dingdong.service.merchant.GoodsSpecificationService;
import com.izouma.dingdong.dto.PageQuery;
import com.izouma.dingdong.exception.BusinessException;
import com.izouma.dingdong.repo.merchant.GoodsSpecificationRepo;
import com.izouma.dingdong.utils.ObjUtils;
import com.izouma.dingdong.utils.excel.ExcelUtils;

import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import java.util.List;

@RestController
@RequestMapping("/goodsSpecification")
@AllArgsConstructor
public class GoodsSpecificationController extends BaseController {

    private GoodsSpecificationService goodsSpecificationService;
    private GoodsSpecificationRepo goodsSpecificationRepo;

    private GoodsRepo goodsRepo;

    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/save")
    @ApiOperation("商品规格添加/修改")
    public GoodsSpecification save(@RequestBody GoodsSpecification record) {
        if (record.getId() != null) {
            GoodsSpecification orig = goodsSpecificationRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
            ObjUtils.merge(orig, record);
            goodsRepo.findById(orig.getGoodsId()).orElseThrow(new BusinessException("商品不存在"));
            return goodsSpecificationRepo.save(orig);
        }
        goodsRepo.findById(record.getGoodsId()).orElseThrow(new BusinessException("商品不存在"));
        return goodsSpecificationRepo.save(record);
    }


    //@PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/all")
    public Page<GoodsSpecification> all(PageQuery pageQuery) {
        return goodsSpecificationRepo.findAll(toSpecification(pageQuery, GoodsSpecification.class), toPageRequest(pageQuery));
    }

    @GetMapping("/get/{id}")
    public GoodsSpecification get(@PathVariable Long id) {
        return goodsSpecificationRepo.findById(id).orElseThrow(new BusinessException("无记录"));
    }

    @PostMapping("/del/{id}")
    public void del(@PathVariable Long id) {
        goodsSpecificationRepo.deleteById(id);
    }

    @GetMapping("/excel")
    @ResponseBody
    public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
        List<GoodsSpecification> data = all(pageQuery).getContent();
        ExcelUtils.export(response, data);
    }

    @GetMapping("/byGoodsId")
    @ApiOperation("按商品ID查询")
    public List<GoodsSpecification> byGoodsId(Long goodsId) {
        return goodsSpecificationRepo.findAllByGoodsId(goodsId);
    }

    @GetMapping("/parent")
    @ApiOperation("按商品ID查出规格分类")
    public List<GoodsSpecification> parent(Long goodsId) {
        return goodsSpecificationRepo.findAllByGoodsIdAndParentIsNull(goodsId);
    }


    @PostMapping("/saveAll")
    @ApiOperation("批量保存规格")
    public List<GoodsSpecification> saveAll(String spec) {
        List<GoodsSpecification> specifications = JSONObject.parseArray(spec, GoodsSpecification.class);
        return goodsSpecificationService.saveAll(specifications);
    }

    @PostMapping("/saveSpecs")
    @ApiOperation("批量保存规格2")
    public List<GoodsSpecification> saveSpecs(String spec) {
        List<GoodsSpecification> specifications = JSONObject.parseArray(spec, GoodsSpecification.class);
        return goodsSpecificationService.save(specifications);
    }
}

