package com.slodon.b2b2c.controller.seller.seller;

import com.slodon.b2b2c.aop.VendorLogger;
import com.slodon.b2b2c.core.constant.StoreCertificateStatusConst;
import com.slodon.b2b2c.core.response.JsonResult;
import com.slodon.b2b2c.core.response.PageVO;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.core.response.SldResponse;
import com.slodon.b2b2c.core.util.AssertUtil;
import com.slodon.b2b2c.core.util.UserUtil;
import com.slodon.b2b2c.core.util.WebUtil;

import com.slodon.b2b2c.goods.dto.StoreCertificateAddDTO;
import com.slodon.b2b2c.goods.dto.StoreCertificateUpdateDTO;
import com.slodon.b2b2c.model.goods.StoreCertificateNewListModel;
import com.slodon.b2b2c.seller.example.StoreCertificateListExample;
import com.slodon.b2b2c.seller.pojo.StoreCertificateList;
import com.slodon.b2b2c.seller.pojo.StoreProfileList;
import com.slodon.b2b2c.seller.pojo.Vendor;
import com.slodon.b2b2c.vo.goods.SellerCertificateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 店铺荣誉资质
 */
@Api(tags = "seller店铺荣誉资质")
@RestController
@RequestMapping("v3/seller/seller/storeCertificate")
public class SellerStoreCertificateController {

    @Resource
    private StoreCertificateNewListModel storeCertificateNewListModel;

    @ApiOperation("店铺荣誉资质列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "webSite", value = "站点", paramType = "query"),
            @ApiImplicitParam(name = "honorName", value = "荣誉资质图片名称", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", defaultValue = "20", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面位置", defaultValue = "1", paramType = "query")
    })
    @GetMapping("list")
    public JsonResult<PageVO<SellerCertificateVo>> getList(HttpServletRequest request,
                                                           @RequestParam(value = "webSite", required = false,defaultValue = "1") String webSite
                                                           ,@RequestParam(value = "honorName", required = false) String honorName){
        Vendor vendor = UserUtil.getUser(request, Vendor.class);
        PagerInfo pager = WebUtil.handlerPagerInfo(request);


        pager.setRowsCount(storeCertificateNewListModel.storeCertificateListPageCount(webSite,honorName,vendor.getStoreId()));
        List<StoreCertificateList> list = storeCertificateNewListModel.getStoreCertificateListByPage(webSite,honorName, pager,vendor.getStoreId());

        List<SellerCertificateVo> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(storeCertificateList -> {
                vos.add(new SellerCertificateVo(storeCertificateList));
            });
        }
        return SldResponse.success(new PageVO<>(vos, pager));
    }


    //test准备用pagehelper的
//    @ApiOperation("店铺荣誉资质列表")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "honorName", value = "荣誉资质图片名称", paramType = "query"),
//    })
//    @GetMapping("list")
//    public JsonResult<PageInfo<SellerCertificateVo>> getNewList(HttpServletRequest request ,
//                                                              @RequestParam(value = "honorName", required = false) String honorName){
//        Vendor vendor = UserUtil.getUser(request, Vendor.class);
//        PagerInfo pager = WebUtil.handlerPagerInfo(request);//分页信息相关page_size
//
//        PageInfo<SellerCertificateVo> pageMessage = storeCertificateNewListModel.getList(vendor,pager,honorName);
//
//
//        return SldResponse.success("ok",pageMessage);
//    }

    @ApiOperation("新增店铺荣誉资质")
    @VendorLogger(option = "新增店铺荣誉资质")
    @PostMapping("add")
    @ResponseBody
    public JsonResult<Integer> addStoreCertificate(HttpServletRequest request, StoreCertificateAddDTO storeCertificateAddDTO) {
        //postman测试数据
//        storeCertificateAddDTO.setHonorName("111");
//        storeCertificateAddDTO.setHonorPath("http://penff");
//        storeCertificateAddDTO.setSort(4);
        String logMsg = "图片名称" + storeCertificateAddDTO.getName();
        // 验证参数是否为空
        AssertUtil.notNull(storeCertificateAddDTO, "品牌信息不能为空，请重试！");
        AssertUtil.notEmpty(storeCertificateAddDTO.getWebSite(), "站点不能为空，请重试！");
        AssertUtil.notEmpty(storeCertificateAddDTO.getName(), "荣誉资质名称不能为空，请重试！");
        AssertUtil.notEmpty(storeCertificateAddDTO.getPath(), "图片路径不能为空，请重试！");
        AssertUtil.notNullOrZero(storeCertificateAddDTO.getSort(), "排序不能为空，请重试！");

        Vendor vendor = UserUtil.getUser(request, Vendor.class);

        storeCertificateNewListModel.saveStoreCertificate(storeCertificateAddDTO, vendor);
        return SldResponse.success("添加成功", logMsg);
    }

    @ApiOperation("编辑店铺荣誉资质")
    @VendorLogger(option = "编辑店铺荣誉资质")
    @PostMapping("edit")
    @ResponseBody
    public JsonResult<Integer> editStoreCertificate(HttpServletRequest request, StoreCertificateUpdateDTO storeCertificateUpdateDTO) {
        //postman测试数据
//        storeCertificateUpdateDTO.setId(2L);
//        storeCertificateUpdateDTO.setHonorName("222");
//        storeCertificateUpdateDTO.setHonorPath("http://www111");
//        storeCertificateUpdateDTO.setSort(3);
        // 验证参数是否为空
        AssertUtil.notNull(storeCertificateUpdateDTO, "品牌信息不能为空，请重试！");
        AssertUtil.notEmpty(storeCertificateUpdateDTO.getWebSite(), "站点不能为空，请重试！");
        AssertUtil.notEmpty(storeCertificateUpdateDTO.getName(), "荣誉资质名称不能为空，请重试！");
        AssertUtil.notEmpty(storeCertificateUpdateDTO.getPath(), "图片路径不能为空，请重试！");
        AssertUtil.notNullOrZero(storeCertificateUpdateDTO.getSort(), "排序不能为空，请重试！");

        Vendor vendor = UserUtil.getUser(request, Vendor.class);

        storeCertificateNewListModel.updateStoreCertificate(storeCertificateUpdateDTO, vendor);
        return SldResponse.success("编辑成功", "店铺荣誉资质id"+storeCertificateUpdateDTO.getId());
    }

    @ApiOperation("删除店铺荣誉资质")
    @VendorLogger(option = "删除店铺荣誉资质")
    @PostMapping("delete")
    public JsonResult<Integer> deleteStoreCertificate(HttpServletRequest request, @RequestParam("id") Long id){
        storeCertificateNewListModel.deleteStoreCertificate(id);
        return SldResponse.success("删除成功","店铺荣誉资质id"+id);
    }






}
