package com.izouma.jiashanxia.web;

import cn.hutool.core.util.ObjectUtil;
import com.izouma.jiashanxia.domain.GoodsInfo;
import com.izouma.jiashanxia.domain.PackageGoods;
import com.izouma.jiashanxia.dto.PackageGoodsDTO;
import com.izouma.jiashanxia.dto.PackageGoodsVO;
import com.izouma.jiashanxia.repo.GoodsInfoRepo;
import com.izouma.jiashanxia.service.PackageGoodsService;
import com.izouma.jiashanxia.dto.PageQuery;
import com.izouma.jiashanxia.exception.BusinessException;
import com.izouma.jiashanxia.repo.PackageGoodsRepo;
import com.izouma.jiashanxia.utils.ObjUtils;
import com.izouma.jiashanxia.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.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/packageGoods")
@AllArgsConstructor
public class PackageGoodsController extends BaseController {
    private PackageGoodsService packageGoodsService;
    private PackageGoodsRepo    packageGoodsRepo;
    private GoodsInfoRepo       goodsInfoRepo;

    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/save")
    public PackageGoods save(@RequestBody PackageGoods record) {
        if (record.getId() != null) {
            PackageGoods orig = packageGoodsRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
            ObjUtils.merge(orig, record);
            return packageGoodsRepo.save(orig);
        }
        return packageGoodsRepo.save(record);
    }


    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/all")
    public Page<PackageGoodsVO> all(@RequestBody PageQuery pageQuery) {
        Map<Long, GoodsInfo> goodsInfoMap = goodsInfoRepo.findAll()
                .stream()
                .collect(Collectors.toMap(GoodsInfo::getId, goods -> goods));
        return packageGoodsService.all(pageQuery).map(packageGoods -> {
            PackageGoodsVO vo = new PackageGoodsVO(packageGoods);
            GoodsInfo goodsInfo = goodsInfoMap.get(packageGoods.getGoodsInfoId());
            if (ObjectUtil.isNotEmpty(goodsInfo)) {
                vo.setName(goodsInfo.getName());
                vo.setUnit(goodsInfo.getUnit());
                vo.setPrice(goodsInfo.getPrice().multiply(BigDecimal.valueOf(packageGoods.getNum())));
            }
            return vo;
        });
    }

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

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

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

    @PostMapping("/getSetGoods")
    @ApiOperation("获取套餐内商品信息")
    public List<PackageGoodsDTO> getSetGoods(@RequestParam Long packageId) {
        return packageGoodsRepo.packageGoods(packageId);
    }
}

