package com.sqx.modules.shop.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.constant.RedisKey;
import com.sqx.common.exception.SqxException;
import com.sqx.common.utils.Constant;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.RedisUtils;
import com.sqx.common.utils.Result;
import com.sqx.datasource.annotation.DataSource;
import com.sqx.modules.app.dao.MsgDao;
import com.sqx.modules.app.dao.UserBrowseDao;
import com.sqx.modules.app.dao.UserDao;
import com.sqx.modules.app.dao.UserMoneyDao;
import com.sqx.modules.app.dao.UserMoneyDetailsDao;
import com.sqx.modules.app.entity.Msg;
import com.sqx.modules.app.entity.UserBrowse;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.entity.UserMoneyDetails;
import com.sqx.modules.app.service.UserMoneyDetailsService;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.common.entity.CommonInfo;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.datacentre.dao.ShopAdminDao;
import com.sqx.modules.datacentre.entity.SysUserShop;
import com.sqx.modules.errand.dao.ErrandComplaintDao;
import com.sqx.modules.errand.dao.ErrandEvaluateDao;
import com.sqx.modules.errand.entity.ErrandComplaint;
import com.sqx.modules.errand.entity.Feedback;
import com.sqx.modules.goods.dao.GoodsShopDao;
import com.sqx.modules.goods.entity.GoodsShop;
import com.sqx.modules.order.dao.AppOrderDao;
import com.sqx.modules.order.entity.TbOrder;
import com.sqx.modules.pay.service.CashOutService;
import com.sqx.modules.shop.dao.ShopMessageDao;
import com.sqx.modules.shop.dto.ShopAuditDTO;
import com.sqx.modules.shop.dto.ShopAuditUpdateDTO;
import com.sqx.modules.shop.service.ShopMessageService;
import com.sqx.modules.sys.dao.SysUserDao;
import com.sqx.modules.sys.entity.SysUserEntity;
import com.sqx.modules.sys.service.SysUserRoleService;
import com.sqx.modules.utils.fieYun.FeiYunUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Slf4j
@Service
public class ShopMessageServiceImpl extends ServiceImpl<ShopMessageDao, GoodsShop> implements ShopMessageService {

    @Autowired
    private ShopMessageDao shopMessageDao;
    @Autowired
    private GoodsShopDao goodsShopDao;
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private ShopAdminDao shopAdminDao;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private CommonInfoService commonInfoService;
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserMoneyDao userMoneyDao;
    @Autowired
    private UserMoneyDetailsDao userMoneyDetailsDao;
    @Autowired
    private MsgDao msgDao;
    @Autowired
    private ErrandComplaintDao errandComplaintDao;
    @Autowired
    private ErrandEvaluateDao errandEvaluateDao;
    @Autowired
    private UserMoneyDetailsService userMoneyDetailsService;
    @Autowired
    private UserBrowseDao userBrowseDao;
    @Autowired
    private CashOutService cashOutService;
    @Autowired
    private UserService userService;
    @Autowired
    private AppOrderDao appOrderDao;

    @Resource
    private RedissonClient redissonClient;

    @Autowired
    private RedisUtils redisUtils;

    @Transactional
    @Override
    public Result insertShopAuthentication(GoodsShop goodsShop) {
        Msg byPhoneAndCode = msgDao.findByPhoneAndCode(goodsShop.getPhone(), goodsShop.getCode());
        if(byPhoneAndCode==null){
            return Result.error("请输入正确的验证码！");
        }
        GoodsShop goodsShop1 = baseMapper.selectOne(new QueryWrapper<GoodsShop>().eq("user_id", goodsShop.getUserId()));
        if (goodsShop1==null){
            goodsShop.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            goodsShop.setStatus(0);
            goodsShop.setCashDeposit(0.00);
            baseMapper.insert(goodsShop);
        }else {
            goodsShop.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            goodsShop.setStatus(0);
            goodsShop.setCashDeposit(0.00);
            goodsShop.setShopId(goodsShop1.getShopId());
            baseMapper.updateById(goodsShop);
            baseMapper.updateAuditReasonIsNull(goodsShop1.getShopId());
        }
        userService.sendMsg(goodsShop.getPhone(),"jieguo");
        return Result.success();
    }

    @Override
    public Result auditShopAuthentication(Long shopId, ShopAuditDTO dto) {
        RLock lock = redissonClient.getLock(String.format(RedisKey.SHOP_AUTHENTICATION_LOCK, shopId));
        lock.lock();
        try {
            return auditAuthentication(shopId, dto);
        } finally {
            lock.unlock();
        }
    }

    @Transactional
    public Result auditAuthentication(Long shopId, ShopAuditDTO dto){
        GoodsShop goodsShop = baseMapper.selectById(shopId);
        if (ObjectUtil.isNull(goodsShop)) {
            return Result.error("店铺不存在！");
        }

        goodsShop.setStatus(dto.getAuditStatus());
        goodsShop.setAuditReason(dto.getAuditReason());
        goodsShop.setShopRate(dto.getShopRate());
        goodsShop.setAuditTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

        // 初始化店铺相关配置
        goodsShop.setPutawayFlag(1);
        goodsShop.setBannedFlag(0);
        goodsShop.setShopSales(0);
        goodsShop.setShopScore(Double.valueOf(5));
        baseMapper.updateById(goodsShop);

        if (Integer.valueOf(Constant.YES).equals(dto.getAuditStatus())) {
            UserEntity userEntity = userService.selectUserById(goodsShop.getUserId());
            SysUserShop sysUserShop = shopAdminDao.selectOne(new QueryWrapper<SysUserShop>().eq("user_id", userEntity.getAdminUserId()));
            if(sysUserShop!=null){
                sysUserShop.setShopId(shopId);
                shopAdminDao.updateById(sysUserShop);
            }else{
                // 添加管理员账号
                SysUserEntity user = sysUserDao.selectOne(new QueryWrapper<SysUserEntity>().eq("username", goodsShop.getPhone()));
                if(user == null){
                    user = new SysUserEntity();
                    user.setUsername(goodsShop.getPhone());
                    user.setMobile(goodsShop.getPhone());
                    List<Long> roleIdList = new ArrayList();
                    roleIdList.add(12L);
                    user.setStatus(1);
                    // sha256加密
                    String salt = RandomStringUtils.randomAlphanumeric(20);
                    user.setPassword(new Sha256Hash(goodsShop.getPhone(), salt).toHex());
                    user.setSalt(salt);
                    user.setUserType(2);
                    user.setCreateTime(new Date());
                    sysUserDao.insertSysUser(user);
                    sysUserRoleService.saveOrUpdate(user.getUserId(), roleIdList);
                    // 返回的主键user_id
                    Long userId = user.getUserId();
                    //添加管理员—商铺关联表
                    sysUserShop = new SysUserShop();
                    sysUserShop.setShopId(shopId);
                    sysUserShop.setUserId(userId);
                    shopAdminDao.insert(sysUserShop);
                }
                // todo 绑定用户和管理用户
                UserEntity newUser = new UserEntity();
                newUser.setUserId(goodsShop.getUserId());
                newUser.setAdminUserId(user.getUserId());
                newUser.setShopAdminFlag(1);
                userDao.updateById(newUser);
            }
            userService.sendMsg(goodsShop.getPhone(),"chenggong");
        } else {
            userService.sendMsg(goodsShop.getPhone(),"shibai");
        }

        return Result.success();
    }

    /*private Result sendMsgDXB(String phone, String value, Integer type) {
        CommonInfo three = commonInfoService.findOne(164);
        CommonInfo four = commonInfoService.findOne(165);
        String testUsername = three.getValue(); //在短信宝注册的用户名
        String testPassword = four.getValue(); //在短信宝注册的密码
        StringBuilder httpArg = new StringBuilder();
        httpArg.append("u=").append(testUsername).append("&");
        httpArg.append("p=").append(Md5Utils.md5s(testPassword)).append("&");
        httpArg.append("m=").append(phone).append("&");
        httpArg.append("c=").append(Md5Utils.encodeUrlString(value, "UTF-8"));
        String result = Md5Utils.request("https://api.smsbao.com/sms", httpArg.toString());
        log.error("短信包返回值："+result);
        return Result.success();
    }*/

    @Override
    public Result selectShopMessage(Long shopId) {
        GoodsShop goodsShop = selectShopId(shopId);
        return Result.success().put("data", goodsShop);
    }

    @Override
    public GoodsShop selectShopId(Long shopId) {
//        TODO  查询主键是不是用selectById()方法更好一点
        GoodsShop goodsShop = baseMapper.selectOne(new QueryWrapper<GoodsShop>().eq("shop_id", shopId));
        if(goodsShop.getShopRate()==null){
            String value = commonInfoService.findOne(300).getValue();
            goodsShop.setShopRate(BigDecimal.valueOf(1).subtract(new BigDecimal(value)));
        }
        return goodsShop;
    }

    @Override
    public Result selectShopList(Integer page, Integer limit, String shopName, Integer status, Integer shopType) {
        Page<GoodsShop> pages = new Page(page, limit);
        PageUtils pageUtils = new PageUtils(goodsShopDao.selectShopList(pages, shopName, status, shopType));
        return Result.success().put("data", pageUtils);
    }

    @Override
    public Result shopEarnings(Long shopId) {
        HashMap<String, Object> hashMap = new HashMap<>();
        String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        BigDecimal shopAllEarnings = goodsShopDao.shopAllEarnings(shopId);
        BigDecimal shopEarningsDay = goodsShopDao.shopEarningsDay(shopId, date);
        BigDecimal shopEarningsMonth = goodsShopDao.shopEarningsMonth(shopId, date);
        hashMap.put("shopAllEarnings", shopAllEarnings);
        hashMap.put("shopEarningsDay", shopEarningsDay);
        hashMap.put("shopEarningsMonth", shopEarningsMonth);
        //用商户id查询用户id
        GoodsShop goodsShop = goodsShopDao.selectOne(new QueryWrapper<GoodsShop>().eq("shop_id", shopId));
        if(goodsShop!=null){
            Double cashMoney= cashOutService.selectCashOutSumsByNowMonth(goodsShop.getUserId());
            hashMap.put("cashMoney", cashMoney);
        }else {
            return Result.error("用户不存在");
        }
        return Result.success().put("data", hashMap);
    }

    @Override
    public Result updateShopMessage(GoodsShop goodsShop) {
        GoodsShop goodsShop1 = goodsShopDao.selectById(goodsShop.getShopId());
        CommonInfo one = commonInfoService.findOne(270);
        if(goodsShop1.getCashDeposit()<Double.parseDouble(one.getValue())){
            return Result.error("保证金不足，不能进行上线操作！");
        }
        if(StringUtils.isNotEmpty(goodsShop.getSnCode())){
            //判断下本次和上次的一样吗 如果一样则不做校验
            if(StringUtils.isNotEmpty(goodsShop1.getSnCode()) && StringUtils.isNotEmpty(goodsShop1.getValue()) ){
                if(!goodsShop1.getSnCode().equals(goodsShop.getSnCode()) || !goodsShop1.getValue().equals(goodsShop.getValue())){
                    String s = addPrinter(goodsShop);
                    if(!"ok".equals(s)){
                        return Result.error(s);
                    }
                    //删除之前的打印机
//                    String snlist = goodsShop1.getSnCode();
//                    FeiYunUtils.deletePrinter(snlist);
                }
            }else{
                String s = addPrinter(goodsShop);
                if(!"ok".equals(s)){
                    return Result.error(s);
                }
            }
        }else{
            //判断之前有没有写过打印机 如果写过 则删除
            if(StringUtils.isNotEmpty(goodsShop1.getSnCode())){
                //删除之前的打印机
//                String snlist = goodsShop1.getSnCode();
//                FeiYunUtils.deletePrinter(snlist);
            }
        }

        // 如果修改为店铺下线状态，需要判断当前店铺有无未完成订单，如有则不允许下线
        // if (Integer.valueOf(Constant.YES).equals(goodsShop.getPutawayFlag())) {
        //     int num = appOrderDao.countUnFinishByShopId(goodsShop.getShopId());
        //     if (num > 0) {
        //         return Result.error("当前店铺存在未完成订单，请完成所有订单后再修改店铺状态");
        //     }
        // }

//        baseMapper.updateById(goodsShop);
        //更新商户号可为null
        baseMapper.update(goodsShop,Wrappers.<GoodsShop>lambdaUpdate()
                .eq(GoodsShop::getShopId,goodsShop.getShopId())
                .set(GoodsShop::getMchId,StringUtils.isEmpty(goodsShop.getMchId())?null:goodsShop.getMchId()));

        redisUtils.delete(String.format(RedisKey.SHOP_INFO_CACHE_KEY, goodsShop.getShopId()));

        return Result.success();
    }


    /**
     * 绑定打印机推送
     *
     * @param goodsShop
     * @return
     */
    public String addPrinter(GoodsShop goodsShop) {
        //目前只有飞鹅pos机可以推送
        if (goodsShop.getFacility()!=null && goodsShop.getFacility().equals(1)) {
            String snlist = goodsShop.getSnCode() + "#" + goodsShop.getValue() + "#" + goodsShop.getShopName();
            String method = FeiYunUtils.addprinter(snlist);
            JSONObject jsonObject = JSON.parseObject(method);
            if (jsonObject != null) {
                if ("0".equals(jsonObject.getString("ret"))) {
                    JSONObject data = jsonObject.getJSONObject("data");
                    JSONArray no = data.getJSONArray("no");
                    if(no.size()>0){
                        String msg = no.getString(0);
                        if(msg.contains("已被添加过")){
                            return "ok";
                        }
                        return no.getString(0);
                    }
                    return "ok";
                }else{
                    return jsonObject.getString("msg");
                }
            }
            return "pos机添加失败！";
        } else {
            return "ok";
        }
    }




    @Override
    @DataSource("dynamic")
    public Result shopHomePage(Long shopId, String startTime, String endTime) {
        HashMap<String, Object> hashMap = new HashMap();
        //总收益
        BigDecimal allMoney = goodsShopDao.allMoney(shopId, startTime, endTime);
        //总销量
        int allCount = goodsShopDao.allCount(shopId, startTime, endTime);
        //用户评价
        Double  countTakingByUserId = errandEvaluateDao.selectEvaluateScoreByShopIdAndTime(shopId, startTime, endTime);
        //订单收入
        BigDecimal orderMoney = goodsShopDao.orderMoney(shopId, startTime, endTime);
        //总订单数
        int sumCountOrder = goodsShopDao.sumCountOrder(shopId, startTime, endTime);
        //退款金额
        BigDecimal refundMoney = goodsShopDao.refundMoney(shopId, startTime, endTime);
        //访客人数
        Integer countByUserId = userBrowseDao.selectCount(new QueryWrapper<UserBrowse>().eq("by_browse_id", shopId));
        //待付款
        int i1 = goodsShopDao.selectCountOrder(shopId, startTime, endTime, 1);
        //制作中
        int i2 = goodsShopDao.selectCountOrder(shopId, startTime, endTime, 6);
        //待取餐
        int i3 = goodsShopDao.selectCountOrder(shopId, startTime, endTime, 3);
        //已完成
        int i4 = goodsShopDao.selectCountOrder(shopId, startTime, endTime, 4);
        //商家待接单
        int i5 = goodsShopDao.selectCountOrder(shopId, startTime, endTime, 7);
        //上架中
        int putawayCount = goodsShopDao.putawayCount(shopId);
        //已下架
        int soldoutCount = goodsShopDao.soldoutCount(shopId);
        hashMap.put("allMoney", allMoney);
        hashMap.put("allCount", allCount);
        hashMap.put("总用户数", null);
        hashMap.put("orderMoney", orderMoney);
        hashMap.put("sumCountOrder", sumCountOrder);
        hashMap.put("refundMoney", refundMoney);
        hashMap.put("访客人数", null);
        hashMap.put("i1", i1);
        hashMap.put("i2", i2);
        hashMap.put("i3", i3);
        hashMap.put("i4", i4);
        hashMap.put("i5", i5);
        hashMap.put("putawayCount", putawayCount);
        hashMap.put("soldoutCount", soldoutCount);
        hashMap.put("countTakingByUserId", countTakingByUserId);
        hashMap.put("countByUserId", countByUserId);
        return Result.success().put("data", hashMap);
    }

    @Transactional
    @Override
    public Result shopRefundCashDeposit(Long shopId) {
        Integer count = appOrderDao.selectCount(new QueryWrapper<TbOrder>().in("status", 3, 6, 7).eq("shop_id", shopId));
        if(count>0){
            return Result.error("当前有未完成的订单，请完成之后进行操作！");
        }
        GoodsShop goodsShop = shopMessageDao.selectById(shopId);
        if(goodsShop.getCashDeposit()<=0){
            return Result.error("你的保证金余额为0，无法退保证金！");
        }
        userMoneyDao.addMoney(goodsShop.getUserId(), BigDecimal.valueOf(goodsShop.getCashDeposit()));
        GoodsShop goodsShop1 = new GoodsShop();
        goodsShop1.setShopId(shopId);
        goodsShop1.setCashDeposit(0.00);
        goodsShop1.setPutawayFlag(1);
        shopMessageDao.updateById(goodsShop1);
        //商户退保证金钱包明细
        UserMoneyDetails userMoneyDetails = new UserMoneyDetails();
        userMoneyDetails.setShopId(shopId);
        userMoneyDetails.setTitle("商户退保证金");
        userMoneyDetails.setType(2);
        userMoneyDetails.setClassify(2);
        userMoneyDetails.setState(2);
        userMoneyDetails.setMoney(BigDecimal.valueOf(goodsShop.getCashDeposit()));
        userMoneyDetails.setContent("商户退保证金："+goodsShop.getCashDeposit()+"元");
        userMoneyDetails.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        userMoneyDetailsDao.insert(userMoneyDetails);
        UserMoneyDetails userMoneyDetails1 = new UserMoneyDetails();
        userMoneyDetails1.setShopId(shopId);
        userMoneyDetails1.setTitle("商户退保证金");
        userMoneyDetails1.setType(1);
        userMoneyDetails1.setClassify(3);
        userMoneyDetails1.setState(2);
        userMoneyDetails1.setMoney(BigDecimal.valueOf(goodsShop.getCashDeposit()));
        userMoneyDetails1.setContent("商户退保证金："+goodsShop.getCashDeposit()+"元，已退回至钱包账户");
        userMoneyDetails1.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        userMoneyDetailsDao.insert(userMoneyDetails1);
        return Result.success();
    }

    @Override
    public Result userFeedback(Feedback feedback) {
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        shopMessageDao.userFeedback(feedback.getShopId(), feedback.getUserEmail(), feedback.getFeedbackScore(), feedback.getFeedbackMessage(), format);
        return Result.success();
    }

    @Override
    public Result selectShopAuthentication(String phone, String msg) {
        Msg byPhone = msgDao.findByPhoneAndCode(phone, msg);
        if(null==byPhone){
            return Result.error("验证码错误！");
        }
        msgDao.deleteById(byPhone.getId());
        GoodsShop goodsShop = shopMessageDao.selectShopAuthentication(phone);
        return Result.success().put("data", goodsShop);
    }

    @Override
    public Result selectShopByUserId(Long userId) {
        GoodsShop goodsShop = shopMessageDao.selectShopByUserId(userId);
        return Result.success().put("data", goodsShop);
    }

    @Override
    public Result selectComplaint(Long shopId, Integer page, Integer limit) {
        Page<ErrandComplaint> pages = new Page<>(page, limit);
        IPage<ErrandComplaint> errandComplaintIPage = errandComplaintDao.selectComplaint(pages, shopId);
        PageUtils pageUtils = new PageUtils(errandComplaintIPage);
        return Result.success().put("data", pageUtils);
    }

    @Override
    public Result updateShopCashDeposit(Long shopId, BigDecimal money, Integer type) {
        goodsShopDao.updateShopCashDeposit(shopId, money, type);
        UserMoneyDetails userMoneyDetails = new UserMoneyDetails();
        userMoneyDetails.setMoney(money);
        userMoneyDetails.setShopId(shopId);
        if(type.equals(1)){
            userMoneyDetails.setContent("赠送商户保证金："+money+"元");
            userMoneyDetails.setTitle("系统赠送商户保证金");
        }else if(type.equals(2)){
            userMoneyDetails.setContent("扣除商户保证金："+money+"元");
            userMoneyDetails.setTitle("系统扣除商户保证金");
        }
        userMoneyDetails.setType(type);
        userMoneyDetails.setClassify(2);
        userMoneyDetails.setState(2);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        userMoneyDetails.setCreateTime(simpleDateFormat.format(new Date()));
        userMoneyDetailsService.save(userMoneyDetails);
        return Result.success();
    }

    @Override
    public Result selectShopCashDepositList(Integer page, Integer limit, Long shopId) {
        Page<UserMoneyDetails> pages = new Page<>(page, limit);
        PageUtils pageUtils = new PageUtils(userMoneyDetailsDao.selectShopCashDepositList(pages, shopId));
        return Result.success().put("data", pageUtils);
    }

    @Override
    public void updateShopAuthentication(Long shopId, ShopAuditUpdateDTO dto) {
        GoodsShop goodsShop = baseMapper.selectById(shopId);
        if (ObjectUtil.isNull(goodsShop)) {
            throw new SqxException("店铺信息不存在");
        }

        boolean isNeedUpdate = false;
        GoodsShop newGoodsShop = new GoodsShop();
        newGoodsShop.setShopId(shopId);
        if (StrUtil.isNotBlank(dto.getShopCover())) {
            newGoodsShop.setShopCover(dto.getShopCover());
            isNeedUpdate = true;
        }

        if (StrUtil.isNotBlank(dto.getRealName())) {
            newGoodsShop.setRealName(dto.getRealName());
            isNeedUpdate = true;
        }

        if (StrUtil.isNotBlank(dto.getIdentityCardNumber())) {
            newGoodsShop.setIdentitycardNumber(dto.getIdentityCardNumber());
            isNeedUpdate = true;
        }

        if (StrUtil.isNotBlank(dto.getPhone())) {
            newGoodsShop.setPhone(dto.getPhone());
            isNeedUpdate = true;
        }

        if (StrUtil.isNotBlank(dto.getBusinessLicense())) {
            newGoodsShop.setBusinessLicense(dto.getBusinessLicense());
            isNeedUpdate = true;
        }

        if (StrUtil.isNotBlank(dto.getIdentityCardPro())) {
            newGoodsShop.setIdentitycardPro(dto.getIdentityCardPro());
            isNeedUpdate = true;
        }

        if (StrUtil.isNotBlank(dto.getIdentityCardCon())) {
            newGoodsShop.setIdentitycardCon(dto.getIdentityCardCon());
            isNeedUpdate = true;
        }

        if (isNeedUpdate) {
            updateById(newGoodsShop);
        }
    }

    @Override
    public GoodsShop getShopInfoById(Long shopId) {
        String key = String.format(RedisKey.SHOP_INFO_CACHE_KEY, shopId);
        String data = (String) redisUtils.get(key);
        if (StrUtil.isNotBlank(data)) {
            return JSONUtil.toBean(data, GoodsShop.class);
        } else {
            GoodsShop goodsShop = getById(shopId);

            redisUtils.set(key, JSONUtil.toJsonStr(goodsShop), RedisUtils.TEN_MINUTE_ONE_EXPIRE);

            return goodsShop;
        }
    }

    @Override
    public Result sendMsgs(String phone, String state) {
        return userService.sendMsg(phone,"ruzhu");
    }

    @Override
    public Result sendMsg(String phone, String state) {
        GoodsShop goodsShop = shopMessageDao.selectShopAuthentication(phone);
        if(goodsShop==null){
            return Result.error("该手机号未申请入驻商铺！");
        }
        CommonInfo name = commonInfoService.findOne(12);
        return userService.sendMsg(goodsShop.getPhone(),"ruzhu");
    }


}
