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.enums.SeoTypeConstant;
import com.slodon.b2b2c.model.seller.StoreProfileModel;
import com.slodon.b2b2c.model.seller.StoreProfileTextListModel;

import com.slodon.b2b2c.seller.dto.StoreProfileListAddDTO;
import com.slodon.b2b2c.seller.dto.StoreProfileListUpdateDTO;
import com.slodon.b2b2c.seller.pojo.*;

import com.slodon.b2b2c.system.pojo.SysSeo;
import com.slodon.b2b2c.vo.seller.StoreProfileListVo;
import com.slodon.b2b2c.vo.seller.StoreProfileTextVo;
import com.slodon.b2b2c.vo.seller.StoreSettingVO;
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.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 店铺简介图片表
 */
@Api(tags = "seller店铺简介图片")
@RequestMapping("v3/seller/seller/storeProfile")
@RestController
public class SellerStoreProfileController {

    @Resource
    private StoreProfileModel storeProfileModel;
    @Resource
    private StoreProfileTextListModel storeProfileTextListModel;


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

        pager.setRowsCount(storeProfileModel.storeProfileListPageCount(webSite,profilePictureName,vendor.getStoreId()));
        List<StoreProfileList> list = storeProfileModel.getStoreProfileListByPage(webSite,profilePictureName, pager,vendor.getStoreId());
        ArrayList<StoreProfileListVo> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(storeProfileList -> {
                vos.add(new StoreProfileListVo(storeProfileList));
            });
        }
        return SldResponse.success(new PageVO<>(vos, pager));
    }


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

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

        storeProfileModel.saveStoreProfile(storeProfileListAddDTO, vendor);
        return SldResponse.success("添加成功", logMsg);
    }

    @ApiOperation("编辑店铺简介图片")
    @PostMapping("edit")
    @ResponseBody
    public JsonResult<Integer> editStoreProfileList(HttpServletRequest request, StoreProfileListUpdateDTO storeProfileListUpdateDTO) {
        //postman测试数据
//        storeProfileListUpdateDTO.setId(7L);
//        storeProfileListUpdateDTO.setName("111--111");
//        storeProfileListUpdateDTO.setPath("http://penff11111");
//        storeProfileListUpdateDTO.setSort(3);
        // 验证参数是否为空
        AssertUtil.notNull(storeProfileListUpdateDTO, "简介图片信息不能为空，请重试！");
        AssertUtil.notEmpty(storeProfileListUpdateDTO.getWebSite(), "站点不能为空，请重试！");
        AssertUtil.notEmpty(storeProfileListUpdateDTO.getName(), "简介图片名称不能为空，请重试！");
        AssertUtil.notEmpty(storeProfileListUpdateDTO.getPath(), "图片路径不能为空，请重试！");
        AssertUtil.notNullOrZero(storeProfileListUpdateDTO.getSort(), "排序不能为空，请重试！");

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

        storeProfileModel.updateStoreProfile(storeProfileListUpdateDTO, vendor);
        return SldResponse.success("编辑成功", "店铺简介图片id"+storeProfileListUpdateDTO.getId());
    }

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


    @ApiOperation("编辑店铺简介内容接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "webSite", value = "站点名称"),
            @ApiImplicitParam(name = "storeDesc", value = "店铺介绍")
    })
    @PostMapping("updateSetting")
    public JsonResult<Integer> updateStoreProfileText(HttpServletRequest request,
                                                      @RequestParam(value = "webSite", required = false) String webSite,
                                                  @RequestParam(value = "storeDesc", required = false) String storeDesc ) throws ParseException {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);
        String logMsg = "店铺id:" + vendor.getStoreId();
        //更新店铺简介内容表
        StoreProfileTextList storeProfileTextList = new StoreProfileTextList();
        storeProfileTextList.setContent(storeDesc);
        storeProfileTextList.setWebSite(webSite);
        //查询店铺简介内容表里是否存在这笔数据
        StoreProfileTextList storeProfileTextInfo = storeProfileTextListModel.getList(vendor.getStoreId(),webSite);
        if (storeProfileTextInfo != null) {//更新
            storeProfileTextList.setUpdateId(vendor.getVendorId());
            storeProfileTextList.setUpdateTime(new Date());
            storeProfileTextListModel.updateProfileText(storeProfileTextList, vendor.getStoreId());
        } else {
            storeProfileTextList.setStoreId(vendor.getStoreId());
            storeProfileTextList.setCreateId(vendor.getVendorId());
            storeProfileTextList.setCreateTime(new Date());
            storeProfileTextListModel.saveProfileText(storeProfileTextList);
        }

        return SldResponse.success("设置成功", logMsg);
    }

    @ApiOperation("获取店铺简介内容接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "webSite", value = "网站名称")
    })
    @GetMapping("settingDetail")
    public JsonResult<StoreProfileTextVo> settingDetail(HttpServletRequest request, @RequestParam(value = "webSite", required = false, defaultValue = "1") String webSite) {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);
//        Store store = storeModel.getStoreByStoreId(vendor.getStoreId());
        StoreProfileTextVo storeProfileTextVo = new StoreProfileTextVo();
        //查询店铺简介内容表里是否存在这笔数据
        StoreProfileTextList storeProfileTextInfo = storeProfileTextListModel.getList(vendor.getStoreId(), webSite);
        if(storeProfileTextInfo!=null){
            storeProfileTextVo.setContent(storeProfileTextInfo.getContent());
            storeProfileTextVo.setWebSite(storeProfileTextInfo.getWebSite());
        }


        return SldResponse.success(storeProfileTextVo);
    }






}
