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

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.model.seller.SellerStoreCompanyShowModel;
import com.slodon.b2b2c.seller.dto.StoreCompanyShowListAddDTO;
import com.slodon.b2b2c.seller.dto.StoreCompanyShowListUpdateDTO;
import com.slodon.b2b2c.seller.pojo.StoreCompanyShowList;
import com.slodon.b2b2c.seller.pojo.Vendor;
import com.slodon.b2b2c.vo.seller.StoreCompanyShowListVo;
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店铺工厂图片")
@RequestMapping("v3/seller/seller/storeCompanyShow")
@RestController
public class SellerStoreCompanyShowController {

    @Resource
    private SellerStoreCompanyShowModel sellerStoreCompanyShowModel;

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

        pager.setRowsCount(sellerStoreCompanyShowModel.storeCompanyShowListPageCount(webSite,companyPictureName,vendor.getStoreId()));
        List<StoreCompanyShowList> list = sellerStoreCompanyShowModel.getStoreCompanyShowListByPage(webSite,companyPictureName, pager,vendor.getStoreId());
        ArrayList<StoreCompanyShowListVo> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(storeCompanyShowList -> {
                vos.add(new StoreCompanyShowListVo(storeCompanyShowList));
            });
        }
        return SldResponse.success(new PageVO<>(vos, pager));
    }

    @ApiOperation("新增店铺工厂图片")
    @PostMapping("add")
    @ResponseBody
    public JsonResult<Integer> addStoreCompanyShowList(HttpServletRequest request, StoreCompanyShowListAddDTO storeCompanyShowListAddDTO) {
        String logMsg = "图片名称" + storeCompanyShowListAddDTO.getName();
        // 验证参数是否为空
        AssertUtil.notNull(storeCompanyShowListAddDTO, "工厂图片信息不能为空，请重试！");
        AssertUtil.notEmpty(storeCompanyShowListAddDTO.getWebSite(), "站点不能为空，请重试！");
        AssertUtil.notEmpty(storeCompanyShowListAddDTO.getName(), "工厂图片名称不能为空，请重试！");
        AssertUtil.notEmpty(storeCompanyShowListAddDTO.getPath(), "图片路径不能为空，请重试！");
        AssertUtil.notNullOrZero(storeCompanyShowListAddDTO.getSort(), "排序不能为空，请重试！");

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

        sellerStoreCompanyShowModel.saveStoreCompanyShow(storeCompanyShowListAddDTO, vendor);
        return SldResponse.success("添加成功", logMsg);
    }

    @ApiOperation("编辑店铺工厂图片")
    @PostMapping("edit")
    @ResponseBody
    public JsonResult<Integer> editStoreCompanyShowList(HttpServletRequest request, StoreCompanyShowListUpdateDTO storeCompanyShowListUpdateDTO) {
        // 验证参数是否为空
        AssertUtil.notNull(storeCompanyShowListUpdateDTO, "工厂图片信息不能为空，请重试！");
        AssertUtil.notEmpty(storeCompanyShowListUpdateDTO.getWebSite(), "站点不能为空，请重试！");
        AssertUtil.notEmpty(storeCompanyShowListUpdateDTO.getName(), "工厂图片名称不能为空，请重试！");
        AssertUtil.notEmpty(storeCompanyShowListUpdateDTO.getPath(), "图片路径不能为空，请重试！");
        AssertUtil.notNullOrZero(storeCompanyShowListUpdateDTO.getSort(), "排序不能为空，请重试！");

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

        sellerStoreCompanyShowModel.updateStoreCompanyShow(storeCompanyShowListUpdateDTO, vendor);
        return SldResponse.success("编辑成功", "店铺工厂图片id"+storeCompanyShowListUpdateDTO.getId());
    }

    @ApiOperation("删除店铺工厂图片")
    @PostMapping("delete")
    public JsonResult<Integer> deleteStoreCompanyShowList(HttpServletRequest request, @RequestParam("id") Long id){
        sellerStoreCompanyShowModel.deleteStoreCompanyShowList(id);
        return SldResponse.success("删除成功","店铺工厂图片id"+id);
    }




}
