package com.slodon.b2b2c.model.seller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.slodon.b2b2c.business.dto.OrderDayDTO;
import com.slodon.b2b2c.business.example.OrderExample;
import com.slodon.b2b2c.core.constant.*;
import com.slodon.b2b2c.core.exception.MallException;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.core.util.*;
import com.slodon.b2b2c.dao.read.business.OrderReadMapper;
import com.slodon.b2b2c.dao.read.goods.GoodsExtendReadMapper;
import com.slodon.b2b2c.dao.read.goods.GoodsReadMapper;
import com.slodon.b2b2c.dao.read.member.MemberFollowStoreReadMapper;
import com.slodon.b2b2c.dao.read.seller.*;
import com.slodon.b2b2c.dao.read.system.*;
import com.slodon.b2b2c.dao.write.goods.GoodsExtendWriteMapper;
import com.slodon.b2b2c.dao.write.goods.GoodsWriteMapper;
import com.slodon.b2b2c.dao.write.seller.*;
import com.slodon.b2b2c.dao.write.system.SysSeoWriteMapper;
import com.slodon.b2b2c.enums.WebSiteConstant;
import com.slodon.b2b2c.goods.example.GoodsExample;
import com.slodon.b2b2c.goods.example.GoodsExtendExample;
import com.slodon.b2b2c.goods.pojo.Goods;
import com.slodon.b2b2c.goods.pojo.GoodsExtend;
import com.slodon.b2b2c.investment.constant.InvestmentConst;
import com.slodon.b2b2c.member.example.MemberFollowStoreExample;
import com.slodon.b2b2c.member.pojo.MemberFollowStore;
import com.slodon.b2b2c.model.goods.GoodsModel;
import com.slodon.b2b2c.model.goods.ProductModel;
import com.slodon.b2b2c.model.system.TplPcMallDecoModel;
import com.slodon.b2b2c.seller.dto.*;
import com.slodon.b2b2c.seller.example.*;
import com.slodon.b2b2c.seller.pojo.*;
import com.slodon.b2b2c.system.example.SysSeoExample;
import com.slodon.b2b2c.system.pojo.Admin;
import com.slodon.b2b2c.system.pojo.SysSeo;
import com.slodon.b2b2c.config.StoreConfig;
import com.slodon.b2b2c.vo.goods.SysSeoVO;
import com.slodon.smartid.client.utils.SmartId;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import static com.slodon.b2b2c.core.constant.StarterConfigConst.MQ_EXCHANGE_NAME;
import static com.slodon.b2b2c.core.constant.StarterConfigConst.MQ_QUEUE_NAME_UNBIND_VIDEO_MEMBER;

@Component
@Slf4j
public class StoreModel {

    @Resource
    private StoreReadMapper storeReadMapper;
    @Resource
    private StoreWriteMapper storeWriteMapper;
    @Resource
    private StoreNameReadMapper storeNameReadMapper;
    @Resource
    private StoreNameWriteMapper storeNameWriteMapper;
    @Resource
    private StoreSiteInfoWriteMapper  storeSiteInfoWriteMapper;
    @Resource
    private VendorReadMapper vendorReadMapper;
    @Resource
    private VendorWriteMapper vendorWriteMapper;
    @Resource
    private StoreApplyWriteMapper storeApplyWriteMapper;
    @Resource
    private StoreCertificateReadMapper storeCertificateReadMapper;
    @Resource
    private StoreCertificateWriteMapper storeCertificateWriteMapper;
    @Resource
    private StoreGradeReadMapper storeGradeReadMapper;
    @Resource
    private StoreBindCategoryWriteMapper storeBindCategoryWriteMapper;
    @Resource
    private StoreCommentReadMapper storeCommentReadMapper;
    @Resource
    private StoreRenewReadMapper storeRenewReadMapper;
    @Resource
    private GoodsReadMapper goodsReadMapper;
    @Resource
    private GoodsWriteMapper goodsWriteMapper;
    @Resource
    private GoodsExtendReadMapper goodsExtendReadMapper;
    @Resource
    private GoodsExtendWriteMapper goodsExtendWriteMapper;
    @Resource
    private MemberFollowStoreReadMapper memberFollowStoreReadMapper;
    @Resource
    private OrderReadMapper orderReadMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private RabbitTemplate rabbitTemplate;

    @Resource
    private SysSeoWriteMapper sysSeoWriteMapper;

    @Resource
    private SysSeoReadMapper sysSeoReadMapper;

    @Resource
    private GoodsModel goodsModel;

    @Resource
    private TplPcMallDecoModel tplPcMallDecoModel;

    @Resource
    private StoreCreateDataModel storeCreateDataModel;

    @Resource
    private StoreConfig storeConfig;

    @Resource
    private VendorModel vendorModel;

    @Resource
    private ProductModel productModel;

    /**
     * 新增店铺表
     *
     * @param store
     * @return
     */
    public Integer saveStore(Store store) {
        int count = storeWriteMapper.insert(store);
        if (count == 0) {
            throw new MallException("添加店铺表失败，请重试");
        }
        return count;
    }

    /**
     * 入驻凭证支付成功，开通店铺
     *
     * @param storeApply
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer openStore(StoreApply storeApply) {

        //新增store表
        Store store = new Store();
        store.setStoreId(storeApply.getStoreId());
        store.setStoreName(storeApply.getStoreName());
        store.setStoreGradeId(storeApply.getStoreGradeId());
        //根据店铺等级id获取等级名称
        StoreGrade storeGrade = storeGradeReadMapper.getByPrimaryKey(storeApply.getStoreGradeId());
        store.setStoreGradeName(storeGrade.getGradeName());
        store.setCreateTime(new Date());
        //店铺到期时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(store.getCreateTime());
        calendar.add(Calendar.YEAR, storeApply.getApplyYear());
        store.setStoreExpireTime(calendar.getTime());
        store.setState(StoreConst.STORE_STATE_OPEN);
        store.setBusinessState(StoreConst.STORE_BUSINESS_STATE_DECORATION);
        store.setIsOwnStore(StoreConst.NO_OWN_STORE);
        store.setOpenTime(storeApply.getApplyYear());

        //查询店铺资质信息
        StoreCertificateExample example = new StoreCertificateExample();
        example.setVendorId(storeApply.getVendorId());
        List<StoreCertificate> list = storeCertificateReadMapper.listByExample(example);
        AssertUtil.notEmpty(list, "获取店铺资质信息为空，请重试");
        StoreCertificate storeCertificate = list.get(0);

        store.setProvinceCode(storeCertificate.getCompanyProvinceCode());
        store.setCityCode(storeCertificate.getCompanyCityCode());
        store.setAreaCode(storeCertificate.getCompanyAreaCode());
        store.setAreaInfo(storeCertificate.getAreaInfo());
        store.setAddress(storeCertificate.getCompanyAddress());

        int count = storeWriteMapper.insert(store);
        if (count == 0) {
            throw new MallException("添加店铺表失败，请重试");
        }

        //新增storeSiteInfo表
        StoreSiteInfo storeSiteInfo = new StoreSiteInfo();
        storeSiteInfo.setStoreId(storeApply.getStoreId());
        storeSiteInfo.setBusinessState(StoreConst.STORE_BUSINESS_STATE_DECORATION);
        storeSiteInfo.setWebSite(WebSiteConstant.MEMBER_DISTRIBUTOR);
        storeSiteInfo.setStoreGradeId(storeApply.getStoreGradeIdCn());
        storeGrade = storeGradeReadMapper.getByPrimaryKey(storeSiteInfo.getStoreGradeId());
        storeSiteInfo.setStoreGradeName(storeGrade != null ? storeGrade.getGradeName() : null);

        if (!StringUtil.isNullOrZero(storeApply.getApplyYearCn())) {
            //开店时长
            storeSiteInfo.setOpenTime(storeApply.getApplyYearCn());
            //店铺到期时间
            calendar = Calendar.getInstance();
            calendar.setTime(store.getCreateTime());
            calendar.add(Calendar.YEAR, storeSiteInfo.getOpenTime());
            storeSiteInfo.setStoreExpireTime(calendar.getTime());
        }

        count = storeSiteInfoWriteMapper.insert(storeSiteInfo);
        if (count == 0) {
            throw new MallException("添加店铺表国内站失败，请重试");
        }

        //修改商家申请表申请状态
        storeApply.setState(StoreConst.STATE_4_STORE_OPEN);
        storeApply.setStoreId(store.getStoreId());
        storeApply.setCallbackTime(new Date());
        count = storeApplyWriteMapper.updateByPrimaryKeySelective(storeApply);
        if (count == 0) {
            throw new MallException("修改店铺申请表失败，请重试");
        }

        //关联store_bind_category与storeId
        StoreBindCategoryExample storeBindCategoryExample = new StoreBindCategoryExample();
        storeBindCategoryExample.setCreateVendorId(storeApply.getVendorId());
        StoreBindCategory storeBindCategory = new StoreBindCategory();
        storeBindCategory.setStoreId(store.getStoreId());
        count = storeBindCategoryWriteMapper.updateByExampleSelective(storeBindCategory, storeBindCategoryExample);
        if (count == 0) {
            throw new MallException("修改店铺可用商品分类申请表失败，请重试");
        }

        JSONObject jsonObject = getStoreUrlsObject(store.getStoreId(), storeApply.getVendorName());

        //关联storeId与vendor表
        Vendor vendor = new Vendor();
        vendor.setVendorId(storeApply.getVendorId());
        vendor.setStoreId(store.getStoreId());
        vendor.setVendorName(storeApply.getVendorName());
        vendor.setStoreUrlBy9710(jsonObject.getStr("9710"));
        vendor.setStoreUrlBy9810(jsonObject.getStr("9810"));
        count = vendorWriteMapper.updateByPrimaryKeySelective(vendor);
        if (count == 0) {
            throw new MallException("修改商家管理员表失败，请重试");
        }

        //复制店铺默认首页,用于入驻店铺初始默认首页 国外站和国内站都复制
        log.info("***** 卖家入驻-复制店铺默认首页，用于入驻店铺初始默认首页 *****");
        storeCreateDataModel.getNewStoreDefaultTpl(vendor, storeApply, WebSiteConstant.MEMBER_OVERSEA);
        storeCreateDataModel.getNewStoreDefaultTpl(vendor, storeApply, WebSiteConstant.MEMBER_DISTRIBUTOR);

        return count;
    }

    /**
     * 新增自营店铺表
     *
     * @param ownStoreAddDTO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer saveStore(OwnStoreAddDTO ownStoreAddDTO) {
        //判断海外店铺名称是否重复
        StoreExample storeExample = new StoreExample();
        storeExample.setStoreName(ownStoreAddDTO.getStoreName());
        storeExample.setStateNotEquals(StoreConst.STORE_STATE_DELETE);
        List<Store> stores = storeReadMapper.listByExample(storeExample);
        if (!CollectionUtils.isEmpty(stores)) {
            throw new MallException("海外站店铺名称已存在，请重新填写");
        }

        //判断国内店铺名称是否重复
        if (WebSiteConstant.MEMBER_DISTRIBUTOR.equals(ownStoreAddDTO.getWebSite())) {
            StoreNameExample storeNameExample = new StoreNameExample();
            storeNameExample.setStoreName(ownStoreAddDTO.getStoreName());
            storeNameExample.setWebSite(ownStoreAddDTO.getWebSite());
            List<StoreName> storeNames = storeNameReadMapper.listByExample(storeNameExample);
            if (!CollectionUtils.isEmpty(storeNames)) {
                throw new MallException("国内站店铺名称已存在，请重新填写");
            }
        }


        //插入store表
        Long storeId = SmartId.nextId(BizTypeConst.STORE);
        Store storeInsert = new Store();
        storeInsert.setStoreId(storeId);
        storeInsert.setStoreName(ownStoreAddDTO.getStoreName());
        storeInsert.setCreateTime(new Date());
        storeInsert.setIsOwnStore(StoreConst.IS_OWN_STORE);
        storeInsert.setState(StoreConst.STORE_STATE_OPEN);
        storeInsert.setBusinessState(StoreConst.STORE_BUSINESS_STATE_DECORATION);
        storeInsert.setOpenTime(100);
        storeInsert.setProvinceCode(ownStoreAddDTO.getProvinceCode());
        storeInsert.setCityCode(ownStoreAddDTO.getCityCode());
        storeInsert.setAreaCode(ownStoreAddDTO.getAreaCode());
        storeInsert.setAreaInfo(ownStoreAddDTO.getAreaInfo());
        if (!StringUtils.isEmpty(ownStoreAddDTO.getAddress())) {
            storeInsert.setAddress(ownStoreAddDTO.getAddress());
        }
        if (!StringUtil.isNullOrZero(ownStoreAddDTO.getBillType()) && !StringUtils.isEmpty(ownStoreAddDTO.getBillDays())) {
            storeInsert.setBillType(ownStoreAddDTO.getBillType());
            //结算日期
            String[] billDayArr = ownStoreAddDTO.getBillDays().split(",");

            boolean flag = false;
            for (String billDay : billDayArr) {
                if (ownStoreAddDTO.getBillType() == StoreConst.BILL_TYPE_WEEK) {
                    for (int i = 1; i < 8; i++) {
                        if (Integer.parseInt(billDay) == i) {
                            flag = true;
                        }
                    }
                } else if (ownStoreAddDTO.getBillType() == StoreConst.BILL_TYPE_MONTH) {
                    for (int i = 1; i < 32; i++) {
                        if (Integer.parseInt(billDay) == i) {
                            flag = true;
                        }
                    }
                }
            }
            if (!flag) {
                throw new MallException("请选择结算周期");
            }

            storeInsert.setBillDay(ownStoreAddDTO.getBillDays());
        }
        int count;
        count = storeWriteMapper.insert(storeInsert);
        if (count == 0) {
            throw new MallException("添加店铺表失败，请重试");
        }

        //插入storeName表和 StoreSiteInfo表(不管新增国内国外站都默认创建两个站点信息)
        StoreName storeNameInsert = new StoreName();
        storeNameInsert.setStoreId(storeId);
        storeNameInsert.setStoreName(ownStoreAddDTO.getStoreName());
        storeNameInsert.setWebSite(WebSiteConstant.MEMBER_DISTRIBUTOR);
        storeNameInsert.setCreateId(0L);
        storeNameInsert.setCreateTime(new Date());
        count = storeNameWriteMapper.insert(storeNameInsert);
        if (count == 0) {
            throw new MallException("添加店铺名称表失败，请重试");
        }
        StoreSiteInfo storeSiteInfo = new StoreSiteInfo();
        storeSiteInfo.setStoreId(storeId);
        storeSiteInfo.setWebSite(WebSiteConstant.MEMBER_DISTRIBUTOR);
        storeSiteInfo.setBusinessState(StoreConst.STORE_BUSINESS_STATE_DECORATION);
        storeSiteInfo.setCreateId(0L);
        storeSiteInfo.setCreateTime(new Date());
        count = storeSiteInfoWriteMapper.insert(storeSiteInfo);
        if (count == 0) {
            throw new MallException("添加店铺站点信息表失败，请重试");
        }

        //插入vendor表，关联storeId
        Vendor vendor = new Vendor();

        //判断管理员名称是否重复
        VendorExample vendorExample = new VendorExample();
        vendorExample.setVendorName(ownStoreAddDTO.getVendorName());
        vendorExample.setIsAllowLoginNotEquals(VendorConst.LOGOUT_ALLOW_LOGIN);
        List<Vendor> vendors = vendorReadMapper.listByExample(vendorExample);
        AssertUtil.isTrue(!CollectionUtils.isEmpty(vendors), "店铺账号已存在，请重新填写");

        //判断管理员手机号是否重复
        VendorExample vendorMobileExample = new VendorExample();
        vendorMobileExample.setVendorMobile(ownStoreAddDTO.getContactPhone());
        vendorMobileExample.setIsAllowLoginNotEquals(VendorConst.LOGOUT_ALLOW_LOGIN);
        List<Vendor> vendorList = vendorReadMapper.listByExample(vendorMobileExample);
        AssertUtil.isTrue(!CollectionUtils.isEmpty(vendorList), "该手机号已被其他账号绑定，请重新填写");

        //获取店铺注册外部接口9710、9810模块url
        JSONObject jsonObject = getStoreUrlsObject(storeId, ownStoreAddDTO.getVendorName());

        vendor.setVendorMobile(ownStoreAddDTO.getContactPhone());
        vendor.setVendorId(SmartId.nextId(BizTypeConst.VENDOR));
        vendor.setVendorName(ownStoreAddDTO.getVendorName());
        vendor.setVendorPassword(Md5.getMd5String(ownStoreAddDTO.getVendorPassword()));
        vendor.setIsStoreAdmin(VendorConst.IS_STORE_ADMIN_1);
        vendor.setStoreId(storeInsert.getStoreId());
        vendor.setRegisterTime(new Date());
        vendor.setLatestLoginTime(new Date());
        vendor.setRolesId(2);
        vendor.setStoreUrlBy9710(jsonObject.getStr("9710"));
        vendor.setStoreUrlBy9810(jsonObject.getStr("9810"));
        count = vendorWriteMapper.insert(vendor);
        if (count == 0) {
            throw new MallException("添加管理员表失败，请重试");
        }

        //插入store_apply表
        StoreApply storeApplyInsert = new StoreApply();
        storeApplyInsert.setVendorId(vendor.getVendorId());
        storeApplyInsert.setVendorName(vendor.getVendorName());
        storeApplyInsert.setStoreId(storeInsert.getStoreId());
        storeApplyInsert.setStoreName(ownStoreAddDTO.getStoreName());
        storeApplyInsert.setState(StoreConst.STATE_4_STORE_OPEN);
        storeApplyInsert.setApplyYear(100);
        storeApplyInsert.setSubmitTime(new Date());
        storeApplyInsert.setStoreType(StoreConst.IS_OWN_STORE);
        count = storeApplyWriteMapper.insert(storeApplyInsert);
        if (count == 0) {
            throw new MallException("添加商家申请表失败，请重试");
        }

        //插入store_certificate表
        StoreCertificate storeCertificate = new StoreCertificate();
        storeCertificate.setVendorId(vendor.getVendorId());
        storeCertificate.setVendorName(vendor.getVendorName());
        storeCertificate.setEnterType(StoreConst.APPLY_TYPE_COMPANY);
        storeCertificate.setCompanyProvinceCode("");
        storeCertificate.setCompanyCityCode("");
        storeCertificate.setCompanyAreaCode("");
        storeCertificate.setAreaInfo("");
        storeCertificate.setCompanyAddress("");
        storeCertificate.setPersonCardUp("");
        storeCertificate.setPersonCardDown("");
        storeCertificate.setContactName(ownStoreAddDTO.getContactName());
        storeCertificate.setContactPhone(ownStoreAddDTO.getContactPhone());
        storeCertificate.setCompanyName(ownStoreAddDTO.getStoreName());
        count = storeCertificateWriteMapper.insert(storeCertificate);
        if (count == 0) {
            throw new MallException("添加店铺资质表失败，请重试");
        }

        //复制店铺默认首页，用于自营店铺初始默认首页
        log.info("***** 创建自营店铺-复制店铺默认首页，用于创建自营店铺初始默认首页 *****");
        storeCreateDataModel.getNewStoreDefaultTpl(vendor, storeApplyInsert, WebSiteConstant.MEMBER_OVERSEA);
        storeCreateDataModel.getNewStoreDefaultTpl(vendor, storeApplyInsert, WebSiteConstant.MEMBER_DISTRIBUTOR);

        return count;
    }

    /**
     * 获取店铺注册外部接口9710、9810模块url
     * @param storeId
     * @param authorName
     * @return
     */
    public JSONObject getStoreUrlsObject(Long storeId, String authorName) {
        HashMap<String, String> headers = new HashMap<>();
        headers.put(InvestmentConst.CONTENT_TYPE, InvestmentConst.APPLICATION_JSON);
        headers.put(InvestmentConst.AUTHORIZATION, storeConfig.getToken());
        JSONObject param = new JSONObject();
        param.set("storeId", storeId.toString());
        param.set("authorName", authorName + "_" + storeId);
        String result;
        try {
            result = HttpClientUtil.httpPost201(storeConfig.getUrl(), param.toString(), headers);
        } catch (Exception e) {
            log.error("调用第三方接口获取店铺URL失败 -请求接口: {}, 请求参数: {},请求头: {}, 错误信息: {}", storeConfig.getUrl(), param.toString(), com.alibaba.fastjson.JSONObject.toJSONString(headers), e.getMessage(), e);
            throw new MallException("获取店铺url信息表失败，请重试");
        }
        JSONObject jsonObject = JSONUtil.parseObj(result);
        jsonObject = jsonObject.getJSONObject("data");
        return jsonObject;
    }

    /**
     * 根据storeId删除店铺表
     *
     * @param storeId storeId
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteStore(Long storeId) {
        //根据storeId查询店铺
        Store store = storeReadMapper.getByPrimaryKey(storeId);
        if (store == null) {
            throw new MallException("店铺不存在");
        }
        //只有关闭的店铺才可以删除
        if (!store.getState().equals(StoreConst.STORE_STATE_CLOSE)) {
            throw new MallException("店铺状态为关闭时才可以删除");
        }

        store.setState(StoreConst.STORE_STATE_DELETE);
        int count = storeWriteMapper.updateByPrimaryKeySelective(store);
        if (count == 0) {
            log.error("根据storeId：" + storeId + "删除店铺表失败");
            throw new MallException("删除店铺表失败,请重试");
        }

        //删除店铺其他站点名称
        StoreNameExample storeNameExample =new StoreNameExample();
        storeNameExample.setStoreId(storeId);
        storeNameWriteMapper.deleteByExample(storeNameExample);

        //查询店铺下的商户账号
        VendorExample vendorExample = new VendorExample();
        vendorExample.setStoreId(storeId);
        List<Vendor> vendors = vendorReadMapper.listByExample(vendorExample);
        for (Vendor vendor : vendors) {
            //删除redis缓存的商户账号权限
            stringRedisTemplate.delete("seller-" + vendor.getVendorId());
        }
        //注销店铺下的商户账号
        Vendor vendorNew = new Vendor();
        vendorNew.setIsAllowLogin(VendorConst.LOGOUT_ALLOW_LOGIN);
        vendorWriteMapper.updateByExampleSelective(vendorNew, vendorExample);

        //删除与店铺关联的store_apply表
        StoreApplyExample storeApplyExample = new StoreApplyExample();
        storeApplyExample.setStoreId(storeId);
        count = storeApplyWriteMapper.deleteByExample(storeApplyExample);
        if (count == 0) {
            throw new MallException("删除商家申请表信息失败，请重试");
        }

        //删除与店铺关联的store_certificate
        vendorExample.setIsStoreAdmin(VendorConst.IS_STORE_ADMIN_1);
        Vendor vendor = vendorReadMapper.listByExample(vendorExample).get(0);
        StoreCertificateExample storeCertificateExample = new StoreCertificateExample();
        storeCertificateExample.setVendorId(vendor.getVendorId());
        count = storeCertificateWriteMapper.deleteByExample(storeCertificateExample);
        if (count == 0) {
            throw new MallException("删除店铺资质信息表失败，请重试");
        }
    }

    /**
     * 根据storeId更新店铺表
     *
     * @param store
     * @return
     */
    public Integer updateStore(Store store) {
        int count = storeWriteMapper.updateByPrimaryKeySelective(store);
        if (count == 0) {
            log.error("根据storeId：" + store.getStoreId() + "更新店铺表失败");
            throw new MallException("更新店铺表失败,请重试");
        }
        return count;
    }

    /**
     * 根据targetId去查询SEO记录
     *
     * @param targetId
     * @return
     */
    public SysSeo getRecordByTargetIdAndType(Long targetId, String type) {
        SysSeo seo = null;
        SysSeoExample seoExample = new SysSeoExample();
        seoExample.createCriteria().andTargetIdEqualTo(targetId).andTypeEqualTo(type);
        List<SysSeo> seoList = sysSeoReadMapper.selectByExample(seoExample);
        if (!CollectionUtils.isEmpty(seoList)) {
            seo = seoList.get(NumConstant.ZERO);
        }
        return seo;
    }


    /**
     *
     * @param targetId
     * @param type
     * @param webSite
     * @return
     */
    public SysSeo getRecordByTargetIdAndType(Long targetId, String type, String webSite) {
        SysSeo seo = null;
        SysSeoExample seoExample = new SysSeoExample();
        seoExample.createCriteria().andTargetIdEqualTo(targetId).andTypeEqualTo(type).andWebSiteEqualTo(webSite);
        List<SysSeo> seoList = sysSeoReadMapper.selectByExample(seoExample);
        if (!CollectionUtils.isEmpty(seoList)) {
            seo = seoList.get(NumConstant.ZERO);
        }
        return seo;
    }

    /**
     * 用于商品详情接口-查询seoVo
     *
     * @param targetId
     * @param type
     * @return
     */
    public SysSeoVO getRecordByTargetIdAndTypeForGoodsDetail(Long targetId, String type) {
        SysSeoVO sysSeoVO = new SysSeoVO();
        SysSeo seo = getRecordByTargetIdAndType(targetId, type);
        Goods goods = goodsModel.getGoodsByGoodsId(targetId);
        if (seo != null) {
            sysSeoVO.setSeoTitle(seo.getTitle().replace("${商品名称}", goods.getGoodsName()));
            sysSeoVO.setSeoDesc(seo.getRemark().replace("${商品广告语}", goods.getGoodsBrief()));
            sysSeoVO.setSeoKeywords(seo.getKeywords().replace("${商品分类}", goods.getCategoryPath()));
        }
        return sysSeoVO;
    }

    /**
     * 用于店铺详情接口-查询seoVo
     *
     * @param targetId
     * @param type
     * @return
     */
    public SysSeoVO getRecordByTargetIdAndTypeForStoreDetail(Long targetId, String type,String  webSite) {
        SysSeoVO sysSeoVO = new SysSeoVO();
        SysSeo seo = getRecordByTargetIdAndType(targetId,type,webSite);
        if (seo != null) {
            sysSeoVO.setSeoTitle(seo.getTitle());
            sysSeoVO.setSeoDesc(seo.getRemark());
            sysSeoVO.setSeoKeywords(seo.getKeywords());
        }
        return sysSeoVO;
    }

    /**
     * 更新SEO表
     *
     * @param sysSeo
     * @return
     */
    public Integer updateSeo(SysSeo sysSeo, Long targetId, String type) {
        SysSeoExample seoExample = new SysSeoExample();
        seoExample.createCriteria().andTargetIdEqualTo(targetId).andTypeEqualTo(type).andWebSiteEqualTo(sysSeo.getWebSite());
        int count = sysSeoWriteMapper.updateByExampleSelective(sysSeo, seoExample);
        if (count == 0) {
            log.error("更新SEO表失败");
            throw new MallException("更新SEO表失败,请重试");
        }
        return count;
    }


    /**
     * 删除SEO
     *
     * @param sysSeo
     * @return
     */
    public Integer deleteSeo(SysSeo sysSeo) {
        SysSeoExample seoExample = new SysSeoExample();
        int count = sysSeoWriteMapper.deleteByExample(seoExample);
        if (count == 0) {
            log.error("删除SEO表失败");
            throw new MallException("删除SEO表失败,请重试");
        }
        return count;
    }

    /**
     * 新增SEO表
     *
     * @param sysSeo
     * @return
     */
    public Integer saveSeo(SysSeo sysSeo) {
        int count = sysSeoWriteMapper.insert(sysSeo);
        if (count == 0) {
            throw new MallException("添加SEO表失败，请重试");
        }
        return count;
    }


//    public Goods getgoodsIdByCategoryIdAndGrade(Integer categoryId , Integer grade){
////        SysSeoExample goodsExample = new SysSeoExample();
////        goodsExample.createCriteria().andTargetIdEqualTo(Integer.valueOf(categoryId).longValue())
////                .andTargetIdEqualTo(Integer.valueOf(grade).longValue());
////        listFieldsByExample
//        Goods goods = goodsModel.getGoodsByGoodsId(targetId);
//
////        goodsExample.cre
//        return null;
//
//    }


    /**
     * 根据storeId更新自营店铺表
     *
     * @param ownStoreUpdateDTO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer updateStore(OwnStoreUpdateDTO ownStoreUpdateDTO) throws Exception {
        //根据storeId获取店铺信息
        Store storeDb = storeReadMapper.getByPrimaryKey(ownStoreUpdateDTO.getStoreId());
        AssertUtil.notNull(storeDb, "未获取到店铺信息");

        //修改store表
        Store storeUpdate = new Store();
        PropertyUtils.copyProperties(storeUpdate, ownStoreUpdateDTO);
        if (!StringUtil.isNullOrZero(ownStoreUpdateDTO.getBillType()) && !StringUtils.isEmpty(ownStoreUpdateDTO.getBillDays())) {
            //结算日期
            String[] billDayArr = ownStoreUpdateDTO.getBillDays().split(",");

            boolean flag = false;
            for (String billDay : billDayArr) {
                if (ownStoreUpdateDTO.getBillType() == StoreConst.BILL_TYPE_WEEK) {
                    for (int i = 1; i < 8; i++) {
                        if (Integer.parseInt(billDay) == i) {
                            flag = true;
                        }
                    }
                } else if (ownStoreUpdateDTO.getBillType() == StoreConst.BILL_TYPE_MONTH) {
                    for (int i = 1; i < 32; i++) {
                        if (Integer.parseInt(billDay) == i) {
                            flag = true;
                        }
                    }
                }
            }
            if (!flag) {
                throw new MallException("请选择结算周期");
            }

            storeUpdate.setBillDay(ownStoreUpdateDTO.getBillDays());
        }

        int count;
        count = storeWriteMapper.updateByPrimaryKeySelective(storeUpdate);
        if (count == 0) {
            log.error("根据storeId：" + storeUpdate.getStoreId() + "更新店铺表失败");
            throw new MallException("更新店铺表失败,请重试");
        }

        //根据storeId获取vendor信息
        VendorExample example = new VendorExample();
        example.setStoreId(ownStoreUpdateDTO.getStoreId());
        example.setIsStoreAdmin(VendorConst.IS_STORE_ADMIN_1);
        Vendor vendorDB = vendorReadMapper.listByExample(example).get(0);

        //若手机号不为空，修改vendor账号手机号
        if (!StringUtils.isEmpty(ownStoreUpdateDTO.getContactPhone())) {
            //判断管理员手机号是否重复
            VendorExample vendorMobileExample = new VendorExample();
            vendorMobileExample.setVendorMobile(ownStoreUpdateDTO.getContactPhone());
            vendorMobileExample.setIsAllowLoginNotEquals(VendorConst.LOGOUT_ALLOW_LOGIN);
            vendorMobileExample.setVendorIdNotEquals(vendorDB.getVendorId());
            List<Vendor> vendorList = vendorReadMapper.listByExample(vendorMobileExample);
            AssertUtil.isTrue(!CollectionUtils.isEmpty(vendorList), "该手机号已被其他账号绑定，请重新填写");
            //不重复，修改管理员手机号
            vendorDB.setVendorMobile(ownStoreUpdateDTO.getContactPhone());
            vendorWriteMapper.updateByPrimaryKeySelective(vendorDB);
        }

        //修改store_certificate表
        StoreCertificate storeCertificateUpdate = new StoreCertificate();
        boolean isCertificateUpdate = false;
        StoreCertificateExample storeCertificateExample = new StoreCertificateExample();
        storeCertificateExample.setVendorId(vendorDB.getVendorId());
        if (!StringUtils.isEmpty(ownStoreUpdateDTO.getContactName())) {
            storeCertificateUpdate.setContactName(ownStoreUpdateDTO.getContactName());
            isCertificateUpdate = true;
        }
        if (!StringUtils.isEmpty(ownStoreUpdateDTO.getContactPhone())) {
            //修改联系人手机号
            storeCertificateUpdate.setContactPhone(ownStoreUpdateDTO.getContactPhone());
            isCertificateUpdate = true;
        }
        if (isCertificateUpdate) {
            count = storeCertificateWriteMapper.updateByExampleSelective(storeCertificateUpdate, storeCertificateExample);
            if (count == 0) {
                throw new MallException("更新商家资质表失败，请重试");
            }
        }
        return count;
    }

    /**
     * 根据storeId修改入驻店铺信息
     *
     * @param storeUpdateDTO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer editStoreInfo(HttpServletRequest request,StoreUpdateDTO storeUpdateDTO) {
        //根据storeId查询store表
        Store storeDB = storeReadMapper.getByPrimaryKey(storeUpdateDTO.getStoreId());
        if (storeDB == null) {
            throw new MallException("店铺不存在");
        }

        //修改store表
        Store storeUpdate = new Store();
        storeUpdate.setStoreId(storeDB.getStoreId());
        storeUpdate.setProvinceCode(storeUpdateDTO.getCompanyProvinceCode());
        storeUpdate.setCityCode(storeUpdateDTO.getCompanyCityCode());
        storeUpdate.setAreaCode(storeUpdateDTO.getCompanyAreaCode());
        storeUpdate.setAreaInfo(storeUpdateDTO.getAreaInfo());
        storeUpdate.setAddress(storeUpdateDTO.getCompanyAddress());
        if (!StringUtils.isEmpty(storeUpdateDTO.getStoreGradeId())) {
            //店铺等级id
            storeUpdate.setStoreGradeId(storeUpdateDTO.getStoreGradeId());
            //根据storeGradeId获取等级名称
            StoreGrade storeGrade = storeGradeReadMapper.getByPrimaryKey(storeUpdateDTO.getStoreGradeId());
            storeUpdate.setStoreGradeName(storeGrade.getGradeName());
        }

        if (!StringUtil.isNullOrZero(storeUpdateDTO.getOpenTime())) {
            //开店时长
            storeUpdate.setOpenTime(storeUpdateDTO.getOpenTime());
            //店铺到期时间
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(storeDB.getCreateTime());
            calendar.add(Calendar.YEAR, storeUpdateDTO.getOpenTime());
            storeUpdate.setStoreExpireTime(calendar.getTime());
        }

        //修改结算周期
        if (!StringUtil.isNullOrZero(storeUpdateDTO.getBillType()) && !StringUtils.isEmpty(storeUpdateDTO.getBillDays())) {
            storeUpdate.setBillType(storeUpdateDTO.getBillType());
            //结算日期
            String[] billDayArr = storeUpdateDTO.getBillDays().split(",");

            //对结算日期进行排序
            List<String> billDaySorted = Arrays.stream(billDayArr).sorted((o1, o2) -> {
                int i1 = Integer.parseInt(o1);
                int i2 = Integer.parseInt(o2);
                return i1 - i2;
            }).collect(Collectors.toList());
            String billDays = String.join(",", billDaySorted);

            boolean billFlag = false;
            for (String billDay : billDayArr) {
                if (storeUpdateDTO.getBillType() == StoreConst.BILL_TYPE_WEEK) {
                    for (int i = 1; i < 8; i++) {
                        if (Integer.parseInt(billDay) == i) {
                            billFlag = true;
                        }
                    }
                } else if (storeUpdateDTO.getBillType() == StoreConst.BILL_TYPE_MONTH) {
                    for (int i = 1; i < 32; i++) {
                        if (Integer.parseInt(billDay) == i) {
                            billFlag = true;
                        }
                    }
                }
            }
            if (!billFlag) {
                throw new MallException("请选择结算周期");
            }
            storeUpdate.setBillDay(billDays);
        }

        int count = storeWriteMapper.updateByPrimaryKeySelective(storeUpdate);
        if (count == 0) {
            log.error("根据storeId：" + storeUpdate.getStoreId() + "更新店铺表失败");
            throw new MallException("更新店铺表失败,请重试");
        }

        if (!CollectionUtils.isEmpty(storeUpdateDTO.getStoreBusinessVOList())) {
            for (StoreBusinessVO storeBusinessVO : storeUpdateDTO.getStoreBusinessVOList()) {
                if (!WebSiteConstant.MEMBER_OVERSEA.equals(storeBusinessVO.getWebSite())) {
                    //修改store_site_info表
                    StoreSiteInfo storeSiteInfoUpdate = new StoreSiteInfo();
                    //店铺等级id
                    storeSiteInfoUpdate.setStoreGradeId(storeBusinessVO.getStoreGradeId());
                    //根据storeGradeId获取等级名称
                    if (storeBusinessVO.getStoreGradeId() != null) {
                        StoreGrade storeGrade = storeGradeReadMapper.getByPrimaryKey(storeSiteInfoUpdate.getStoreGradeId());
                        storeSiteInfoUpdate.setStoreGradeName(storeGrade != null ? storeGrade.getGradeName() : null);
                    }



                    //开店时长
                    storeSiteInfoUpdate.setOpenTime(storeBusinessVO.getOpenTime());
                    //店铺到期时间
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(storeDB.getCreateTime());
                    calendar.add(Calendar.YEAR, storeBusinessVO.getOpenTime());
                    storeSiteInfoUpdate.setStoreExpireTime(calendar.getTime());

                    Admin admin = UserUtil.getUser(request, Admin.class);
                    storeSiteInfoUpdate.setUpdateId(admin.getAdminId().longValue());
                    storeSiteInfoUpdate.setUpdateTime(new Date());
                    StoreSiteInfoExample storeSiteInfoExample = new StoreSiteInfoExample();
                    storeSiteInfoExample.setStoreId(storeUpdateDTO.getStoreId());
                    storeSiteInfoExample.setWebSite(WebSiteConstant.MEMBER_DISTRIBUTOR);
                    count = storeSiteInfoWriteMapper.updateByExampleSelective(storeSiteInfoUpdate, storeSiteInfoExample);
                    if (count == 0) {
                        log.error("根据storeId：" + storeSiteInfoUpdate.getStoreId() + "更新店铺表国内站失败");
                        throw new MallException("更新店铺表国内站失败,请重试");
                    }
                }
            }
        }
        //修改store_apply表
        StoreApply storeApplyUpdate = new StoreApply();
        boolean isUpdate = false;
        StoreApplyExample storeApplyExample = new StoreApplyExample();
        storeApplyExample.setStoreId(storeUpdateDTO.getStoreId());
        if (!StringUtils.isEmpty(storeUpdateDTO.getStoreGradeId())) {
            storeApplyUpdate.setStoreGradeId(storeUpdateDTO.getStoreGradeId());
            isUpdate = true;
        }
        if (!StringUtils.isEmpty(storeUpdateDTO.getOpenTime())) {
            storeApplyUpdate.setApplyYear(storeUpdateDTO.getOpenTime());
            isUpdate = true;
        }
        if (isUpdate) {
            count = storeApplyWriteMapper.updateByExampleSelective(storeApplyUpdate, storeApplyExample);
            if (count == 0) {
                throw new MallException("更新商家申请表失败，请重试");
            }
        }

        //根据storeId查询vendor表
        VendorExample vendorExample = new VendorExample();
        vendorExample.setStoreId(storeDB.getStoreId());
        vendorExample.setIsStoreAdmin(VendorConst.IS_STORE_ADMIN_1);
        Vendor vendor = vendorReadMapper.listByExample(vendorExample).get(0);

        //修改store_certificate表
        StoreCertificate storeCertificateUpdate = new StoreCertificate();
        if (!StringUtils.isEmpty(storeUpdateDTO.getContactPhone())) {
            //不重复，修改联系人手机号
            storeCertificateUpdate.setContactPhone(storeUpdateDTO.getContactPhone());
        }
        StoreCertificateExample storeCertificateExample = new StoreCertificateExample();
        storeCertificateExample.setVendorId(vendor.getVendorId());
        storeCertificateUpdate.setContactName(storeUpdateDTO.getContactName());
        storeCertificateUpdate.setContactPhone(storeUpdateDTO.getContactPhone());
        storeCertificateUpdate.setCompanyProvinceCode(storeUpdateDTO.getCompanyProvinceCode());
        storeCertificateUpdate.setCompanyCityCode(storeUpdateDTO.getCompanyCityCode());
        storeCertificateUpdate.setCompanyAreaCode(storeUpdateDTO.getCompanyAreaCode());
        storeCertificateUpdate.setAreaInfo(storeUpdateDTO.getAreaInfo());
        storeCertificateUpdate.setCompanyAddress(storeUpdateDTO.getCompanyAddress());
        storeCertificateUpdate.setPersonCardUp(storeUpdateDTO.getPersonCardUp());
        storeCertificateUpdate.setPersonCardDown(storeUpdateDTO.getPersonCardDown());
        storeCertificateUpdate.setCompanyName(storeUpdateDTO.getCompanyName());
        storeCertificateUpdate.setBusinessLicenseImage(storeUpdateDTO.getBusinessLicenseImage());
        storeCertificateUpdate.setMoreQualification1(storeUpdateDTO.getMoreQualification1());
        storeCertificateUpdate.setMoreQualification2(storeUpdateDTO.getMoreQualification2());
        storeCertificateUpdate.setMoreQualification3(storeUpdateDTO.getMoreQualification3());
        count = storeCertificateWriteMapper.updateByExampleSelective(storeCertificateUpdate, storeCertificateExample);
        if (count == 0) {
            throw new MallException("更新商家资质表失败，请重试");
        }
        return count;
    }

    /**
     * 根据storeId关闭/开启店铺
     *
     * @param storeId 店铺id
     * @param state   店铺状态： 1、开启；2、关闭
     * @return
     */
    public void lockUpStore(Long storeId, Integer state) {
        //根据storeId查询店铺
        Store store = storeReadMapper.getByPrimaryKey(storeId);
        AssertUtil.notNull(store, "店铺不存在");

        Store storeNew = new Store();
        storeNew.setStoreId(storeId);
        if (state == StoreConst.STORE_STATE_CLOSE) {
            //关闭店铺
            storeNew.setState(StoreConst.STORE_STATE_CLOSE);
            this.updateStore(storeNew);

            //下架店铺的商品
            GoodsExample example = new GoodsExample();
            example.setStoreId(storeId);
            List<Goods> list = goodsReadMapper.listByExample(example);
            if (CollectionUtils.isEmpty(list)) {
                return;
            }

            Goods goods = new Goods();
            goods.setState(GoodsConst.GOODS_STATE_LOWER_BY_SYSTEM);
            goods.setUpdateTime(new Date());
            goodsWriteMapper.updateByExampleSelective(goods, example);
            //修改商品更新时间
            GoodsExtendExample extendExample = new GoodsExtendExample();
            extendExample.setStoreId(storeId);
            GoodsExtend goodsExtend = new GoodsExtend();
            goodsExtend.setOfflineReason("店铺关闭");
            goodsExtend.setOfflineComment("平台后台操作");
            goodsExtendWriteMapper.updateByExampleSelective(goodsExtend, extendExample);

            list.forEach(goods1 -> {
                VendorExample vendorExample = new VendorExample();
                vendorExample.setStoreId(storeId);
                vendorExample.setIsStoreAdmin(VendorConst.IS_STORE_ADMIN_1);
                List<Vendor> vendorList = vendorModel.getVendorList(vendorExample, null);
                AssertUtil.isTrue(CollectionUtils.isEmpty(vendorList), "获取店铺信息失败");
                goods1.setState(GoodsConst.GOODS_STATE_LOWER_BY_SYSTEM);
                productModel.upsertProduct(goods1, vendorList.get(0));
            });

            //直播模块开启,解除视频带货绑定会员
            if ("1".equals(stringRedisTemplate.opsForValue().get("live_switch"))) {
                rabbitTemplate.convertAndSend(MQ_EXCHANGE_NAME, MQ_QUEUE_NAME_UNBIND_VIDEO_MEMBER, storeId);
            }
        } else {
            //开启店铺
            storeNew.setState(StoreConst.STORE_STATE_OPEN);
            this.updateStore(storeNew);
        }
    }

    /**
     * 根据storeId获取店铺表详情
     *
     * @param storeId storeId
     * @return
     */
    public Store getStoreByStoreId(Long storeId) {
        return storeReadMapper.getByPrimaryKey(storeId);
    }

    /**
     * 根据条件获取店铺表列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<Store> getStoreList(StoreExample example, PagerInfo pager) {
        List<Store> storeList;
        if (pager != null) {
            pager.setRowsCount(storeReadMapper.countByExample(example));
            storeList = storeReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            storeList = storeReadMapper.listByExample(example);
        }
        return storeList;
    }

    /**
     * 根据条件获取店铺区域统计列表
     *
     * @param example 查询条件信息
     * @return
     */
    public List<StoreStatisticsDTO> getStoreStatisticList(StoreExample example) {
        return storeReadMapper.getStoreStatisticList(example);
    }

    /**
     * 定时任务设定商家的评分
     *
     * @return
     */
    public boolean jobSetStoreScore() {
        StoreExample example = new StoreExample();
        example.setState(StoreConst.STORE_STATE_OPEN);
        List<Store> storeList = storeReadMapper.listByExample(example);
        if (!CollectionUtils.isEmpty(storeList)) {
            for (Store store : storeList) {
                CommentsDTO commentsDto = storeCommentReadMapper.getStoreScoreSum(store.getStoreId());
                if (commentsDto != null && commentsDto.getNumber() != null && commentsDto.getNumber() > 0) {
                    Store storeNew = new Store();
                    storeNew.setStoreId(store.getStoreId());
                    BigDecimal descriptionScore = (new BigDecimal(commentsDto.getDescription()))
                            .divide((new BigDecimal(commentsDto.getNumber())), 1, BigDecimal.ROUND_HALF_UP);
                    storeNew.setDescriptionScore(descriptionScore.toString());

                    BigDecimal serviceScore = (new BigDecimal(commentsDto.getServiceAttitude()))
                            .divide((new BigDecimal(commentsDto.getNumber())), 1, BigDecimal.ROUND_HALF_UP);
                    storeNew.setServiceScore(serviceScore.toString());

                    BigDecimal deliverGoodsScore = (new BigDecimal(commentsDto.getDeliverSpeed())).divide(
                            (new BigDecimal(commentsDto.getNumber())), 1, BigDecimal.ROUND_HALF_UP);
                    storeNew.setDeliverScore(deliverGoodsScore.toString());

                    int update = storeWriteMapper.updateByPrimaryKeySelective(storeNew);
                    AssertUtil.isTrue(update == 0, "修改商家评分时失败：storeId=" + store.getStoreId());
                }
            }
        }
        return true;
    }

    /**
     * 定时任务设定商家各项统计数据
     *
     * @return
     */
    public boolean jobStoreStatistics() {
        StoreExample example = new StoreExample();
        example.setState(StoreConst.STORE_STATE_OPEN);
        List<Store> storeList = storeReadMapper.listByExample(example);
        if (!CollectionUtils.isEmpty(storeList)) {
            for (Store store : storeList) {
                Store storeNew = new Store();
                storeNew.setStoreId(store.getStoreId());
                // 商品数量
                GoodsExample goodsExample = new GoodsExample();
                goodsExample.setStoreId(store.getStoreId());
                goodsExample.setState(GoodsConst.GOODS_STATE_UPPER);
                storeNew.setGoodsNumber(goodsReadMapper.countByExample(goodsExample));

                // 店铺收藏
                MemberFollowStoreExample example2 = new MemberFollowStoreExample();
                example2.setStoreId(store.getStoreId());
                List<MemberFollowStore> memberFollowStoreList = memberFollowStoreReadMapper.listByExample(example2);
                storeNew.setFollowNumber(memberFollowStoreList.size());

                // 店铺总销售金额、店铺完成订单量
                OrderExample orderExample = new OrderExample();
                orderExample.setStoreId(store.getStoreId());
                orderExample.setOrderState(OrderConst.ORDER_STATE_40);
                List<OrderDayDTO> dtoList = orderReadMapper.getOrderDayDto(orderExample);

                if (!CollectionUtils.isEmpty(dtoList)) {
                    BigDecimal orderAmount = BigDecimal.ZERO;
                    BigDecimal refundAmount = BigDecimal.ZERO;
                    Integer count = 0;
                    for (OrderDayDTO orderDayDto : dtoList) {
                        orderAmount = orderAmount.add(orderDayDto.getOrderAmount());
                        refundAmount = refundAmount.add(orderDayDto.getRefundAmount());
                        count += orderDayDto.getCount();
                    }
                    storeNew.setStoreTotalSale(orderAmount.subtract(refundAmount));
                    storeNew.setOrderFinishedCount(count);
                }

                // 店铺总订单量
                orderExample.setOrderState(null);
                storeNew.setOrderTotalCount(orderReadMapper.countByExample(orderExample));
                int update = storeWriteMapper.updateByPrimaryKeySelective(storeNew);
                AssertUtil.isTrue(update == 0, "统计商家数据时失败：storeId=" + store.getStoreId());
            }
        }
        return true;
    }

    /**
     * 系统自动完成对店铺的冻结或续签管理
     *
     * @return
     */
    public List<Store> jobSystemStoreManage() {
        List<Store> stores = new ArrayList<>();
        //查询审核通过状态的store，判断店铺是否到期
        StoreExample example = new StoreExample();
        example.setState(StoreConst.STORE_STATE_OPEN);
        example.setIsOwnStore(StoreConst.NO_OWN_STORE);
        List<Store> storeList = storeReadMapper.listByExample(example);

        //所有店铺共用一个固定的到期时限提醒
        int reminderDay = Integer.parseInt(stringRedisTemplate.opsForValue().get("store_expired_reminder_day"));
        //计算到期日期
        Date expireTime = TimeUtil.getDateApartDay(-reminderDay);

        if (!CollectionUtils.isEmpty(storeList)) {
            for (Store store : storeList) {
                Date storeExpireTime = store.getStoreExpireTime();
                //只通知一次
                if (storeExpireTime.before(expireTime) && storeExpireTime.after(TimeUtil.getDateApartDay(expireTime, -1))) {
                    stores.add(store);
                    continue;
                }

                Date startTime = TimeUtil.changeFormatYMD(storeExpireTime);
                Date endTime = TimeUtil.changeFormatYMDHMS(storeExpireTime);
                //店铺到期，查询续签表
                StoreRenewExample renewExample = new StoreRenewExample();
                renewExample.setStoreId(store.getStoreId());
                renewExample.setState(StoreConst.STORE_RENEW_STATE_SUCCESS);
                renewExample.setStartTimeAfter(startTime);
                List<StoreRenew> storeRenewList = storeRenewReadMapper.listByExample(renewExample);
                if (CollectionUtils.isEmpty(storeRenewList)) {
                    //续签表中没有符合条件的续签申请，冻结店铺，冻结店铺的商品
                    //判断是否今天到期
                    if (startTime.before(new Date()) && endTime.after(startTime)) {
                        //冻结店铺
                        Store storeNew = new Store();
                        storeNew.setStoreId(store.getStoreId());
                        storeNew.setState(StoreConst.STORE_STATE_CLOSE);
                        int row = storeWriteMapper.updateByPrimaryKeySelective(storeNew);
                        AssertUtil.isTrue(row == 0, "冻结店铺时失败！");

                        //冻结店铺的商品
                        GoodsExample goodsExample = new GoodsExample();
                        goodsExample.setStoreId(store.getStoreId());
                        List<Goods> list = goodsReadMapper.listByExample(goodsExample);
                        if (CollectionUtils.isEmpty(list)) {
                            continue;
                        }

                        Goods goods = new Goods();
                        goods.setState(GoodsConst.GOODS_STATE_LOWER_BY_STORE);
                        goods.setUpdateTime(new Date());
                        row = goodsWriteMapper.updateByExampleSelective(goods, goodsExample);
                        AssertUtil.isTrue(row == 0, "冻结店铺商品时失败！");

                    }
                } else {
                    //续签表中有符合条件的续签申请，续签店铺，修改store表的店铺等级、开店时长、开店结束时间
                    StoreRenew storeRenew = storeRenewList.get(0);
                    Store updateStore = new Store();
                    updateStore.setStoreId(store.getStoreId());
                    updateStore.setStoreGradeId(storeRenew.getGradeId());
                    updateStore.setOpenTime(storeRenew.getDuration());
                    updateStore.setStoreExpireTime(storeRenew.getEndTime());
                    int row = storeWriteMapper.updateByPrimaryKeySelective(updateStore);
                    AssertUtil.isTrue(row == 0, "更新店铺信息时失败！");
                }
            }
        }
        return stores;
    }

    /**
     * 定时更新店铺商品总销量、总浏览量
     *
     * @return
     */
    public boolean jobSetStoreSalesAndLookVolume() {
        //获取所有开启状态的店铺
        StoreExample storeExample = new StoreExample();
        storeExample.setState(StoreConst.STORE_STATE_OPEN);
        List<Store> storeList = storeReadMapper.listByExample(storeExample);
        if (!CollectionUtils.isEmpty(storeList)) {
            for (Store store : storeList) {
                //更新每一个店铺商品总销量、总浏览量
                Store storeUpdate = new Store();
                storeUpdate.setStoreId(store.getStoreId());
                //店铺商品总销量
                int allSaleNum = 0;
                //店铺商品总浏览量
                int allLookNum = 0;
                GoodsExample goodsExample = new GoodsExample();
                goodsExample.setStoreId(store.getStoreId());
                List<Goods> goodsList = goodsReadMapper.listByExample(goodsExample);
                if (!CollectionUtils.isEmpty(goodsList)) {
                    for (Goods goods : goodsList) {
                        int saleNum = goods.getActualSales() + goods.getVirtualSales();
                        allSaleNum += saleNum;

                        GoodsExtendExample goodsExtendExample = new GoodsExtendExample();
                        goodsExtendExample.setGoodsId(goods.getGoodsId());
                        GoodsExtend goodsExtend = goodsExtendReadMapper.listByExample(goodsExtendExample).get(0);
                        allLookNum += goodsExtend.getClickNumber();
                    }
                }
                //获取店铺商品总销量
                storeUpdate.setStoreSalesVolume(allSaleNum);
                //获取店铺商品总浏览量
                storeUpdate.setStoreLookVolume(allLookNum);
                int update = storeWriteMapper.updateByPrimaryKeySelective(storeUpdate);
                AssertUtil.isTrue(update == 0, "更新店铺商品总销量、总浏览量时失败：storeId=" + storeUpdate.getStoreId());
            }
        }
        return true;
    }

    /**
     * 根据条件获取店铺数量
     *
     * @param example 查询条件信息
     * @return
     */
    public Integer getStoreCount(StoreExample example) {
        return storeReadMapper.countByExample(example);
    }

    public List<Store> getStoreCnList(StoreExample example, PagerInfo pager) {
        pager.setRowsCount(storeReadMapper.countCnByExample(example));
        return storeReadMapper.listCnPageByExample(example, pager.getStart(), pager.getPageSize());
    }
}