package com.slodon.b2b2c.model.seller;

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.StoreCompanyShowListReadMapper;
import com.slodon.b2b2c.dao.write.seller.StoreCompanyShowListWriteMapper;
import com.slodon.b2b2c.goods.dto.StoreCertificateUpdateDTO;
import com.slodon.b2b2c.seller.dto.StoreCompanyShowListAddDTO;
import com.slodon.b2b2c.seller.dto.StoreCompanyShowListUpdateDTO;
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.StoreCertificateList;
import com.slodon.b2b2c.seller.pojo.StoreCompanyShowList;
import com.slodon.b2b2c.seller.pojo.StoreProfileList;
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 SellerStoreCompanyShowModel {

    @Resource
    private StoreCompanyShowListReadMapper storeCompanyShowListReadMapper;
    @Resource
    private StoreCompanyShowListWriteMapper storeCompanyShowListWriteMapper;

    /**
     *店铺分页总条数
     * @param webSite
     * @param companyPictureName 工厂照片名称
     * @param storeId 店铺id
     * @return
     */
    public Integer storeCompanyShowListPageCount(String webSite,String companyPictureName , Long storeId){
        return storeCompanyShowListReadMapper.storeCompanyShowPageCount(webSite,companyPictureName,storeId);
    }

    /**
     *店铺工厂图片列表
     * @param webSite
     * @param companyPictureName 查询条件
     * @param pager 分页信息
     * @return
     */
    public List<StoreCompanyShowList> getStoreCompanyShowListByPage(String webSite,String companyPictureName , PagerInfo pager,Long storeId){
        List<StoreCompanyShowList> list = null;
        if(pager!= null){
            list = storeCompanyShowListReadMapper.storeCompanyShowListPage(webSite,companyPictureName,pager.getStart(),pager.getPageSize(),storeId);
        }
        return list;
    }


    /**
     * 店铺工厂图片的新增
     * @param storeCompanyShowListAddDTO 店铺工厂图片的新增入参
     * @param vendor
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer saveStoreCompanyShow(StoreCompanyShowListAddDTO storeCompanyShowListAddDTO , Vendor vendor){
        //店铺工厂图片名称是否重复
//        StoreCompanyShowListExample example = new StoreCompanyShowListExample();
//        example.createCriteria().andNameEqualTo(storeCompanyShowListAddDTO.getName()).andStatusEqualTo(StoreCompanyShowConstant.COMPANY_NORMAL).andCreateIdEqualTo(vendor.getVendorId());
//        List<StoreCompanyShowList> list = storeCompanyShowListReadMapper.selectByExample(example);
//        AssertUtil.isTrue(!CollectionUtils.isEmpty(list), "店铺工厂图片名称已存在，请重新上传图片时重命名");

        //新增入库
        StoreCompanyShowList storeCompanyShowList = new StoreCompanyShowList();
        storeCompanyShowList.setWebSite(storeCompanyShowListAddDTO.getWebSite());
        storeCompanyShowList.setStoreId(vendor.getStoreId());
        storeCompanyShowList.setName(storeCompanyShowListAddDTO.getName());
        storeCompanyShowList.setPath(storeCompanyShowListAddDTO.getPath());
        storeCompanyShowList.setSort(storeCompanyShowListAddDTO.getSort());
        storeCompanyShowList.setCreateId(vendor.getVendorId());
        storeCompanyShowList.setCreateTime(new Date());
        storeCompanyShowList.setStatus(StoreCompanyShowConstant.COMPANY_NORMAL);
        int count = storeCompanyShowListWriteMapper.insert(storeCompanyShowList);
        if(count == 0){
            throw new MallException("新增店铺工厂图片失败，请重试");
        }
        return count;
    }

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

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

    /**
     *删除店铺工厂图片
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteStoreCompanyShowList(Long id){
        //先根据id查询到店铺工厂图片表的这笔记录
        StoreCompanyShowList storeCompanyShowListInfo = getStoreCompanyShowInfo(id);
        //将这笔数据的状态置为delete
        storeCompanyShowListInfo.setStatus(StoreCompanyShowConstant.COMPANY_DELETE);
        int count = storeCompanyShowListWriteMapper.updateByPrimaryKeySelective(storeCompanyShowListInfo);
        if(count == 0){
            throw new MallException("删除店铺工厂图片失败，请重试");
        }
        return count;
    }

    /**
     * 根据storeId和状态获取对应店铺工厂图片信息
     * @param storeId
     * @return
     */
    public List<StoreCompanyShowList> getStoreCompanyShowByStoreIdAndStatus(Long storeId,String webSite){
        StoreCompanyShowListExample example = new StoreCompanyShowListExample();
        example.createCriteria().andStatusEqualTo(StoreCompanyShowConstant.COMPANY_NORMAL).andStoreIdEqualTo(storeId).andWebSiteEqualTo(webSite);
        return storeCompanyShowListReadMapper.selectByExample(example);
    }



}
