package com.slodon.b2b2c.model.seller;

import com.slodon.b2b2c.core.constant.StoreCertificateStatusConst;
import com.slodon.b2b2c.core.constant.StoreCompanyShowConstant;
import com.slodon.b2b2c.core.exception.MallException;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.core.util.AssertUtil;
import com.slodon.b2b2c.dao.read.seller.StoreProfileListReadMapper;
import com.slodon.b2b2c.dao.write.seller.StoreProfileListWriteMapper;
import com.slodon.b2b2c.seller.dto.StoreCompanyShowListAddDTO;
import com.slodon.b2b2c.seller.dto.StoreCompanyShowListUpdateDTO;
import com.slodon.b2b2c.seller.dto.StoreProfileListAddDTO;
import com.slodon.b2b2c.seller.dto.StoreProfileListUpdateDTO;
import com.slodon.b2b2c.seller.example.StoreCertificateListExample;
import com.slodon.b2b2c.seller.example.StoreCompanyShowListExample;
import com.slodon.b2b2c.seller.example.StoreProfileListExample;
import com.slodon.b2b2c.seller.pojo.StoreCompanyShowList;
import com.slodon.b2b2c.seller.pojo.StoreProfileList;
import com.slodon.b2b2c.seller.pojo.StoreProfileTextList;
import com.slodon.b2b2c.seller.pojo.Vendor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 店铺工厂图片表model
 */
@Component
@Slf4j
public class StoreProfileModel {

    @Resource
    private StoreProfileListReadMapper storeProfileListReadMapper;
    @Resource
    private StoreProfileListWriteMapper storeProfileListWriteMapper;
    /**
     *店铺简介照片列表分页总条数
     * @param profilePictureName 简介照片名称
     * @param storeId 店铺id
     * @return
     */
    public Integer storeProfileListPageCount(String webSite,String profilePictureName , Long storeId){
        return storeProfileListReadMapper.storeProfileListPageCount(webSite,profilePictureName,storeId);
    }

    /**
     *店铺简介图片列表
     * @param profilePictureName 查询条件
     * @param pager 分页信息
     * @return
     */
    public List<StoreProfileList> getStoreProfileListByPage(String webSite,String profilePictureName , PagerInfo pager, Long storeId){
        List<StoreProfileList> list = null;
        if(pager!= null){
            list = storeProfileListReadMapper.storeProfileListPage(webSite,profilePictureName,pager.getStart(),pager.getPageSize(),storeId);
        }
        return list;
    }

    /**
     * 店铺简介图片的新增
     * @param storeProfileListAddDTO 店铺简介图片的新增入参
     * @param vendor
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer saveStoreProfile(StoreProfileListAddDTO storeProfileListAddDTO , Vendor vendor){
        //店铺工厂图片名称是否重复
        StoreProfileListExample example = new StoreProfileListExample();
        example.createCriteria().andNameEqualTo(storeProfileListAddDTO.getName()).andStatusEqualTo(StoreCompanyShowConstant.COMPANY_NORMAL).andCreateIdEqualTo(vendor.getVendorId()).andWebSiteEqualTo(storeProfileListAddDTO.getWebSite());
        List<StoreProfileList> list = storeProfileListReadMapper.selectByExample(example);
        AssertUtil.isTrue(!CollectionUtils.isEmpty(list), "公司介绍图片名称已存在，请重新上传图片时重命名");

        //新增入库
        StoreProfileList storeProfileList = new StoreProfileList();
        storeProfileList.setWebSite(storeProfileListAddDTO.getWebSite());
        storeProfileList.setStoreId(vendor.getStoreId());
        storeProfileList.setName(storeProfileListAddDTO.getName());
        storeProfileList.setPath(storeProfileListAddDTO.getPath());
        storeProfileList.setSort(storeProfileListAddDTO.getSort());
        storeProfileList.setCreateId(vendor.getVendorId());
        storeProfileList.setCreateTime(new Date());
        //因常量类内容一致此处引用StoreCompanyShowConstant
        storeProfileList.setStatus(StoreCompanyShowConstant.COMPANY_NORMAL);
        int count = storeProfileListWriteMapper.insert(storeProfileList);
        if(count == 0){
            throw new MallException("新增店铺简介图片失败，请重试");
        }
        return count;
    }


    /**
     * 店铺简介图片的更新
     * @param storeProfileListUpdateDTO 店铺简介图片的更新入参
     * @param vendor
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer updateStoreProfile(StoreProfileListUpdateDTO storeProfileListUpdateDTO, Vendor vendor) {
        //根据id查询到店铺简介图片表的这笔记录
        StoreProfileList storeProfileListInfo  = getStoreProfileInfo(storeProfileListUpdateDTO.getId());
        //页面上的id是肯定存在与库中的，不需要考虑storeCompanyShowListInfo是否为空
        storeProfileListInfo.setName(storeProfileListUpdateDTO.getName());
        storeProfileListInfo.setPath(storeProfileListUpdateDTO.getPath());
        storeProfileListInfo.setWebSite(storeProfileListUpdateDTO.getWebSite());
        storeProfileListInfo.setUpdateId(vendor.getVendorId());
        storeProfileListInfo.setUpdateTime(new Date());
        storeProfileListInfo.setSort(storeProfileListUpdateDTO.getSort());
        int count = storeProfileListWriteMapper.updateByPrimaryKey(storeProfileListInfo);
        if(count == 0){
            throw new MallException("修改店铺工厂图片表失败，请重试");
        }
        return count;
    }

    /**
     * 根据id查询到店铺工厂图片表的这笔记录
     * @param id
     * @return
     */
    public StoreProfileList getStoreProfileInfo(Long id){
        return storeProfileListReadMapper.selectByPrimaryKey(id);
    }


    /**
     *删除店铺简介图片
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteStoreProfileList(Long id){
        //先根据id查询到店铺简介图片表的这笔记录
        StoreProfileList storeProfileListInfo  = getStoreProfileInfo(id);
        //将这笔数据的状态置为delete
        //因常量类内容一致此处引用StoreCompanyShowConstant
        storeProfileListInfo.setStatus(StoreCompanyShowConstant.COMPANY_DELETE);
        int count = storeProfileListWriteMapper.updateByPrimaryKeySelective(storeProfileListInfo);
        if(count == 0){
            throw new MallException("删除店铺简介图片失败，请重试");
        }
        return count;
    }

    /**
     * 根据storeId和状态获取对应店铺简介图片信息
     * @param storeId
     * @return
     */
    public List<StoreProfileList> getStoreProfileByStoreIdAndStatus(Long storeId, String webSite){
        StoreProfileListExample example = new StoreProfileListExample();
        example.createCriteria().andStatusEqualTo(StoreCompanyShowConstant.COMPANY_NORMAL).andStoreIdEqualTo(storeId).andWebSiteEqualTo(webSite);
        return storeProfileListReadMapper.selectByExample(example);
    }




}
