package com.izouma.dingdong.web.merchant;


import com.izouma.dingdong.enums.ApplyStatus;
import com.izouma.dingdong.enums.GoodType;
import com.izouma.dingdong.repo.merchant.GoodsSpecificationRepo;
import com.izouma.dingdong.service.merchant.MerchantService;
import com.izouma.dingdong.utils.SecurityUtils;
import com.izouma.dingdong.web.BaseController;
import com.izouma.dingdong.domain.merchant.Goods;
import com.izouma.dingdong.service.merchant.GoodsService;
import com.izouma.dingdong.dto.PageQuery;
import com.izouma.dingdong.exception.BusinessException;
import com.izouma.dingdong.repo.merchant.GoodsRepo;
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;
import java.util.Map;

@RestController
@RequestMapping("/goods")
@AllArgsConstructor
public class GoodsController extends BaseController {
    private GoodsService goodsService;

    private GoodsRepo goodsRepo;

    private MerchantService merchantService;

    private GoodsSpecificationRepo goodsSpecificationRepo;

    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/save")
    @ApiOperation("添加修改商品")
    public Goods save(@RequestBody Goods record) {
        if (record.getId() != null) {
           /* Goods orig = goodsRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
            //如果修改商品名称和图片就重新审核
            if (record.getName() != null || record.getImg() != null) {
                record.setStatus(ApplyStatus.PENDING);
            }
            //如果修改价格，购物车内的商品就要重新计算
            ObjUtils.merge(orig, record);
            return goodsService.save(orig);*/
            goodsService.change(record);
        }
        return goodsService.save(record);
    }


    //后台商品列表
    //@PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/all")
    public Page<Goods> all(PageQuery pageQuery) {
        return goodsRepo.findAll(toSpecification(pageQuery, Goods.class), toPageRequest(pageQuery));
    }

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

    @PostMapping("/del/{id}")
    public void del(@PathVariable Long id) {
        goodsRepo.deleteById(id);
        //规格一并被删除
        goodsSpecificationRepo.deleteByGoodsId(id);
    }

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

    @GetMapping("/my")
    public List<Goods> my() {
        Long merchantId = merchantService.findMerchantId(SecurityUtils.getAuthenticatedUser().getId());
        return goodsRepo.findAllByMerchantId(merchantId);
        //return goodsRepo.findAllByMerchantIdAndIsPassTrue(merchantId);
    }

    @GetMapping("/take")
    @ApiOperation("上下架商品")
    public void take(Long id) {
        goodsService.take(id);
    }

    @GetMapping("/audit")
    @ApiOperation("审核商品")
    public Goods audit(Long id, Boolean pass, String reason) {
        return goodsService.audit(id, pass, reason);
    }

    @GetMapping("/getInfo/{id}")
    @ApiOperation("商品信息/商品规格/商品评价")
    public Map<String, Object> getInfo(@PathVariable Long id) {
        return goodsService.goodsAllInfo(id);
    }


    @GetMapping("/showAll")
    @ApiOperation("发现美食下商品所有信息")
    public List<Goods> showAll(GoodType type, Double longitude, Double latitude) {
        //type 发现美食标签， 以及经纬度
        if (type == null) {
            type = GoodType.SIGNBOARD_FOOD;
        }
        //3KM 以内
        return goodsService.showAll(type, longitude, latitude, 3000.0);
    }

    @GetMapping("/goods")
    @ApiOperation("/客户端下的商家内的商品列表")
    public List<Goods> goods(Long merchantId) {
        return goodsRepo.findAllByMerchantIdAndStatus(merchantId, ApplyStatus.PASS);
    }

}

