package com.cweb.controller.goods;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.security.SessionObject;
import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.PageUtil;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.GoodsMsg;
import com.hfkj.entity.GoodsSpecs;
import com.hfkj.entity.GoodsType;
import com.hfkj.entity.GoodsVpd;
import com.hfkj.model.ResponseData;
import com.hfkj.model.SecUserSessionObject;
import com.hfkj.model.goods.GoodsModel;
import com.hfkj.model.goods.GoodsModelSpecs;
import com.hfkj.model.goods.GoodsTypeModel;
import com.hfkj.service.goods.*;
import com.hfkj.sysenum.SecUserObjectTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Controller
@RequestMapping(value="/goods")
@Api(value="商品信息")
public class GoodsController {

    private static final Logger log = LoggerFactory.getLogger(GoodsController.class);

    @Resource
    private GoodsTypeService goodsTypeService;

    @Resource
    private GoodsBrandService goodsBrandService;

    @Resource
    private GoodsMsgService goodsMsgService;

    @Resource
    private GoodsSpecsService goodsSpecsService;

    @Resource
    private GoodsVpdService goodsVpdService;


    @RequestMapping(value="/getListGoodsType",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询商品分类列表")
    public ResponseData getListGoodsType() {
        try {

            Map<String ,Object> map = new HashMap<>();
            map.put("status" , 1);
            List<GoodsType> list = goodsTypeService.getList(map);

            List<GoodsTypeModel> goodsTypeModels = goodsTypeService.getGoodsTypeModelList(list);

            return ResponseMsgUtil.success(goodsTypeModels);

        } catch (Exception e) {
            log.error("error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/getListBrand",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询品牌列表")
    public ResponseData getListBrand(@RequestParam(value = "title" , required = false) String title,
                                     @RequestParam(value = "ext1" , required = false) String ext1) {
        try {

            Map<String , Object> map = new HashMap<>();
            map.put("title", title);

            JSONObject object = new JSONObject();
            object.put("list", goodsBrandService.getList(map));
            map.put("ext1", "热门");
            object.put("listExt", goodsBrandService.getList(map));

            return ResponseMsgUtil.success(object);

        } catch (Exception e) {
            log.error("error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/getListGoods", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询商品列表")
    public ResponseData getListGoods(
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "goodsTypeParent", required = false) Long goodsTypeParent,
            @RequestParam(value = "goodsType", required = false) Long goodsType,
            @RequestParam(value = "goodsBrand", required = false) Long goodsBrand,
            @RequestParam(value = "price", required = false) Integer price,
            @RequestParam(value = "saleNum", required = false) Integer saleNum,
            @RequestParam(value = "time", required = false) Integer time,
            @RequestParam(name = "pageNum", required = true) Integer pageNum,
            @RequestParam(name = "pageSize", required = true) Integer pageSize
    ) {
        try {


            Map<String, Object> map = new HashMap<>();

            map.put("title", title);
            map.put("goodsType", goodsType);
            map.put("goodsTypeParent", goodsTypeParent);
            map.put("goodsBrand", goodsBrand);
            map.put("saleNum", saleNum);
            map.put("time", time);
            map.put("status", 1);

            List<GoodsMsg> list = goodsMsgService.getList(map);
            List<GoodsSpecs> specsList = goodsSpecsService.getList(new HashMap<>());

            List<GoodsModel> goodsModels = new ArrayList<>();

            for (GoodsMsg goodsMsg : list) {

                GoodsModel goodsModel = new GoodsModel();

                List<GoodsSpecs>  goodsSpecs = specsList.stream().filter(s -> s.getGoodsId().equals(goodsMsg.getId())).collect(Collectors.toList());

                if (!goodsSpecs.isEmpty()) {
                    BigDecimal minPrice = goodsSpecs.get(0).getSalePrice();
                    BigDecimal minOriginalPrice = goodsSpecs.get(0).getOriginalPrice();

                    for (GoodsSpecs specs : goodsSpecs) {
                        if (specs.getSalePrice().compareTo(minPrice) < 0) {
                            minPrice = specs.getSalePrice();
                            minOriginalPrice = specs.getOriginalPrice();
                        }
                    }
                    BeanUtils.copyProperties(goodsMsg, goodsModel);
                    goodsModel.setOriginalPrice(minOriginalPrice);
                    goodsModel.setPrice(minPrice);

                    goodsModels.add(goodsModel);

                }
            }

            if (price != null) {
                if (price == 1) {
                    goodsModels = goodsModels.stream().sorted(Comparator.comparing(GoodsModel::getPrice)).collect(Collectors.toList());
                }
                if (price == 2) {
                    goodsModels = goodsModels.stream().sorted(Comparator.comparing(GoodsModel::getPrice).reversed()).collect(Collectors.toList());
                }
            }

            return ResponseMsgUtil.success(PageUtil.initPageInfoObj(pageNum, goodsModels.size(), pageSize, new PageInfo<>(goodsModels)));

        } catch (Exception e) {
            log.error("GoodsDetailController --> getListUser() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }



    @RequestMapping(value="/getGoodsDetail",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询商品详情")
    public ResponseData getGoodsDetail(@RequestParam(value = "id" , required = false) Long id) {
        try {

            GoodsMsg goodsMsg = goodsMsgService.queryDetail(id);

            GoodsModel goodsModel = new GoodsModel();

            BeanUtils.copyProperties(goodsMsg, goodsModel);

            Map<String , Object> map = new HashMap<>();
            map.put("goodsId" , id);

            List<GoodsSpecs> goodsSpecs = goodsSpecsService.getList(map);

            List<GoodsModelSpecs> goodsModelSpecs = new ArrayList<>();

            for (GoodsSpecs goodsSpec : goodsSpecs) {
                GoodsModelSpecs goodsModelSpec = new GoodsModelSpecs();
                BeanUtils.copyProperties(goodsSpec, goodsModelSpec);
                GoodsVpd goodsVpd = goodsVpdService.queryDetailBySpecsId(goodsSpec.getId());
                if (goodsVpd != null) {
                    BeanUtils.copyProperties(goodsVpd, goodsModelSpec);
                }
                goodsModelSpec.setSpecsId(goodsSpec.getId());
                goodsModelSpecs.add(goodsModelSpec);
            }

            goodsModel.setGoodsModelSpecs(goodsModelSpecs);

            return ResponseMsgUtil.success(goodsModel);

        } catch (Exception e) {
            log.error("error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

}
