package com.slodon.b2b2c.model.goods;


import com.slodon.b2b2c.core.constant.GoodsConst;
import com.slodon.b2b2c.core.constant.StoreCertificateStatusConst;
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.StoreCertificateListReadMapper;
import com.slodon.b2b2c.dao.write.seller.StoreCertificateListWriteMapper;
import com.slodon.b2b2c.goods.dto.StoreCertificateAddDTO;
import com.slodon.b2b2c.goods.dto.StoreCertificateUpdateDTO;
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.models.auth.In;
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.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 店铺荣誉资质信息表model
 */
@Component
@Slf4j
public class StoreCertificateNewListModel {

    @Resource
    private StoreCertificateListReadMapper storeCertificateListReadMapper;

    @Resource
    private StoreCertificateListWriteMapper storeCertificateListWriteMapper;

    /**
     *店铺荣誉资质列表分页总条数
     * @param webSite 站点
     * @param honorName 简介照片名称
     * @param storeId 店铺id
     * @return
     */
    public Integer storeCertificateListPageCount(String webSite,String honorName , Long storeId){
        return storeCertificateListReadMapper.storeCertificateListPageCount(webSite,honorName,storeId);
    }

    /**
     *店铺荣誉资质列表
     * @param webSite 站点
     * @param honorName 查询条件
     * @param pager 分页信息
     * @return
     */
    public List<StoreCertificateList> getStoreCertificateListByPage(String webSite,String honorName , PagerInfo pager, Long storeId){
        List<StoreCertificateList> list = null;
        if(pager!= null){
            list = storeCertificateListReadMapper.getStoreCertificateListByPage(webSite,honorName,pager.getStart(),pager.getPageSize(),storeId);
        }
        return list;
    }

    //test
//    public PageInfo<SellerCertificateVo> getList(Vendor vendor , PagerInfo pager, String honorName){

//        StoreCertificateListExample storeCertificateListExample = new StoreCertificateListExample();
//        storeCertificateListExample.createCriteria().andHonorNameEqualTo(honorName)
//                .andStoreIdEqualTo(vendor.getStoreId()).andStatusEqualTo(StoreCertificateStatusConst.CER_NORMAL);
//        storeCertificateListExample.setOrderByClause("sort desc");
//        // 1. 分页的开始页的分页设置，分页一定要传递这两个参数
//        PageHelper.startPage(pager.getStart(), pager.getPageSize());
//        // 2. 紧跟分页设置后的第一条SQL语句会被分页查询，在SQL语句后自动拼接limit子句
//        List<StoreCertificateList> certificateVoList = storeCertificateListReadMapper.selectByExample(storeCertificateListExample);
//        PageInfo<SellerCertificateVo> pageInfo = new PageInfo(certificateVoList);
//
//        return pageInfo;
//
//    }


    /**
     * 新增荣誉资质店铺
     * @param storeCertificateAddDTO
     * @param vendor
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer saveStoreCertificate(StoreCertificateAddDTO storeCertificateAddDTO, Vendor vendor) {
        //判断荣誉资质图片名称是否重复
        StoreCertificateListExample example = new StoreCertificateListExample();
        example.createCriteria().andHonorNameEqualTo(storeCertificateAddDTO.getName()).andStatusEqualTo(StoreCertificateStatusConst.CER_NORMAL).andCreateIdEqualTo(vendor.getVendorId()).andWebSiteEqualTo(storeCertificateAddDTO.getWebSite());
        List<StoreCertificateList> list = storeCertificateListReadMapper.selectByExample(example);
        AssertUtil.isTrue(!CollectionUtils.isEmpty(list), "荣誉资质名称已存在，请重新上传图片时重命名");

        //插入店铺荣誉资质表
        StoreCertificateList storeCertificateInfo = new StoreCertificateList();
        storeCertificateInfo.setWebSite(storeCertificateAddDTO.getWebSite());
        storeCertificateInfo.setStoreId(vendor.getStoreId());
        storeCertificateInfo.setHonorName(storeCertificateAddDTO.getName());
        storeCertificateInfo.setHonorPath(storeCertificateAddDTO.getPath());
        storeCertificateInfo.setCreateId(vendor.getVendorId());
        storeCertificateInfo.setCreateTime(new Date());
        storeCertificateInfo.setSort(storeCertificateAddDTO.getSort());
        storeCertificateInfo.setStatus(StoreCertificateStatusConst.CER_NORMAL);
        int count = storeCertificateListWriteMapper.insert(storeCertificateInfo);
        if(count == 0){
            throw new MallException("新增店铺荣誉资质失败，请重试");
        }
        return count;
    }


    /**
     * 更新店铺荣誉资质表
     * @param storeCertificateUpdateDTO
     * @param vendor
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer updateStoreCertificate(StoreCertificateUpdateDTO storeCertificateUpdateDTO, Vendor vendor) {
        //更新店铺荣誉资质表
        StoreCertificateList storeCertificateInfo = getStoreCertificateInfo(storeCertificateUpdateDTO.getId());
//        storeCertificateInfo.setId(storeCertificateUpdateDTO.getId());
//        storeCertificateInfo.setStoreId(vendor.getStoreId());
        storeCertificateInfo.setWebSite(storeCertificateUpdateDTO.getWebSite());
        storeCertificateInfo.setHonorName(storeCertificateUpdateDTO.getName());
        storeCertificateInfo.setHonorPath(storeCertificateUpdateDTO.getPath());
        storeCertificateInfo.setUpdateId(vendor.getVendorId());
        storeCertificateInfo.setUpdateTime(new Date());
        storeCertificateInfo.setSort(storeCertificateUpdateDTO.getSort());
//        storeCertificateInfo.setStatus(StoreCertificateStatusConst.CER_NORMAL);
        int count = storeCertificateListWriteMapper.updateByPrimaryKey(storeCertificateInfo);
        if(count == 0){
            throw new MallException("修改店铺荣誉资质失败，请重试");
        }
        return count;
    }


    /**
     * 根据资质id删除（逻辑，只是改状态）店铺资质信息
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteStoreCertificate(Long id){
        //先根据id查到这笔数据
        StoreCertificateList storeCertificateInfo = getStoreCertificateInfo(id);
        storeCertificateInfo.setStatus(StoreCertificateStatusConst.CER_DELETE);
        int count = storeCertificateListWriteMapper.updateByPrimaryKeySelective(storeCertificateInfo);
        if(count == 0){
            throw new MallException("删除店铺荣誉资质失败，请重试");
        }
        return count;
    }


    /**
     * 根据id查资质荣誉信息
     * @param id
     * @return
     */
    public StoreCertificateList getStoreCertificateInfo(Long id){
        return storeCertificateListReadMapper.selectByPrimaryKey(id);
    }


    /**
     * 根据店铺ID查询到此店铺下的所有荣誉资质信息
     * @param storeId
     * @return
     */
    public List<StoreCertificateList> getStoreCertificateByStoreId(Long storeId,String webSite){
        StoreCertificateListExample example = new StoreCertificateListExample();
        example.createCriteria().andStatusEqualTo(StoreCertificateStatusConst.CER_NORMAL).andStoreIdEqualTo(storeId).andWebSiteEqualTo(webSite);
        return storeCertificateListReadMapper.selectByExample(example);
    }




}
