package com.kmall.admin.service.impl.mk.store;

import com.kmall.admin.dao.GoodsDao;
import com.kmall.admin.dao.mk.store.MkStorePromChnlDao;
import com.kmall.admin.dao.mk.store.MkStorePromDao;
import com.kmall.admin.dao.mk.store.MkStorePromTypeDao;
import com.kmall.admin.dao.mk.store.StoreTopicDao;
import com.kmall.admin.entity.GoodsEntity;
import com.kmall.admin.entity.mk.store.MkStorePromChnlEntity;
import com.kmall.admin.entity.mk.store.MkStorePromEntity;
import com.kmall.admin.entity.mk.store.MkStorePromTypeEntity;
import com.kmall.admin.entity.mk.store.StoreTopicEntity;
import com.kmall.admin.fromcomm.entity.SysUserEntity;
import com.kmall.admin.service.mk.store.MkStorePromService;
import com.kmall.admin.utils.ShiroUtils;
import com.kmall.common.constant.Dict;
import com.kmall.common.utils.DateUtils;
import com.kmall.common.utils.RRException;
import com.kmall.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 门店推广Service实现类
 *
 * @author huangyq
 * @email admin@qhdswl.com
 * @date 2019-04-26 15:52:20
 */
@Service("mkStorePromService")
public class MkStorePromServiceImpl implements MkStorePromService {
    @Autowired
    private MkStorePromDao mkStorePromDao;
    @Autowired
    private StoreTopicDao storeTopicDao;
    @Autowired
    private MkStorePromTypeDao mkStorePromTypeDao;
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private MkStorePromChnlDao mkStorePromChnlDao;


    @Override
    public MkStorePromEntity queryObject(Integer promId) {
        MkStorePromEntity mkStorePromEntity = mkStorePromDao.queryObject(promId);
        if(mkStorePromEntity != null) {
            if (mkStorePromEntity.getPromType().equalsIgnoreCase(Dict.promType.item_10.getItem())) {
                GoodsEntity goodsEntity = goodsDao.queryObject(mkStorePromEntity.getGoodsId());
                mkStorePromEntity.setGoodsName(goodsEntity.getName());
            }
            if(mkStorePromEntity.getPromType().equalsIgnoreCase(Dict.promType.item_00.getItem())){
                StoreTopicEntity storeTopicEntity = storeTopicDao.queryObject(mkStorePromEntity.getStoreTopicId());
                mkStorePromEntity.setTopicTitle(storeTopicEntity.getTitle());
            }
            String startDate = DateUtils.format(mkStorePromEntity.getPromBegTime(), DateUtils.DATE_TIME_PATTERN);
            String endDate = DateUtils.format(mkStorePromEntity.getPromEndTime(), DateUtils.DATE_TIME_PATTERN);
            String inValidDate = startDate + " - " + endDate;
            mkStorePromEntity.setInValidDate(inValidDate);

            MkStorePromChnlEntity chnlEntity = mkStorePromChnlDao.queryObject(mkStorePromEntity.getPromChnlId());
            mkStorePromEntity.setPromChnName(chnlEntity.getPromChnName());
        }
        return mkStorePromEntity;
    }

    @Override
    public List<MkStorePromEntity> queryList(Map<String, Object> map) {
        List<MkStorePromEntity> mkStorePromList = mkStorePromDao.queryList(map);
        for (MkStorePromEntity mkStorePromEntity : mkStorePromList) {
            String promStat = "";
            String promInfo = "";
            String promPicUrl = "";

            String browseNum = StringUtils.isNotEmpty(String.valueOf(mkStorePromEntity.getBrowseNum())) ? String.valueOf(mkStorePromEntity.getBrowseNum()) : "0";
            String payOrderNum = StringUtils.isNotEmpty(String.valueOf(mkStorePromEntity.getPayOrderNum())) ? String.valueOf(mkStorePromEntity.getPayOrderNum()) : "0";
            String actMoney = StringUtils.isNotEmpty(String.valueOf(mkStorePromEntity.getActMoney())) ? String.valueOf(mkStorePromEntity.getActMoney()) : "0";
            String startDate = DateUtils.format(mkStorePromEntity.getPromBegTime(), DateUtils.DATE_TIME_PATTERN);
            String endDate = DateUtils.format(mkStorePromEntity.getPromEndTime(), DateUtils.DATE_TIME_PATTERN);
            String inValidDate = startDate + " - " + endDate;
            if(mkStorePromEntity.getPromType().equalsIgnoreCase(Dict.promType.item_00.getItem())){
                promStat = "浏览量：" + browseNum;
                promInfo = "<span style=\"font-weight: bolder\">" + mkStorePromEntity.getPromName() + "</span>\n"
                        + mkStorePromEntity.getTopicTitle() + "\n推广有效期："
                        + inValidDate ;

                /*if(mkStorePromEntity.getIsFinished().equalsIgnoreCase(Dict.isFinished.item_1.getItem())){
                    promInfo = promInfo + "&nbsp;&nbsp;&nbsp;&nbsp;<i-button type=\"info\" style=\"margin-top: -3px;\" onclick='vm.updateFinished(" + mkStorePromEntity.getPromId() + ")'>立即结束</i-button>";
                }*/
                promPicUrl = mkStorePromEntity.getItemPicUrl();
            }
            if(mkStorePromEntity.getPromType().equalsIgnoreCase(Dict.promType.item_10.getItem())){
                promStat = "浏览量：" + browseNum + "\n支付单数：" + payOrderNum + "\n实收金额：" + actMoney;
                promInfo = "<span style=\"font-weight: bolder\">" + mkStorePromEntity.getPromName() + "</span>\n"
                        + mkStorePromEntity.getGoodsName() + "\n推广有效期："
                        + inValidDate;
                /*if(mkStorePromEntity.getIsFinished().equalsIgnoreCase(Dict.isFinished.item_1.getItem())){
                    promInfo = promInfo + "&nbsp;&nbsp;&nbsp;&nbsp;<i-button type=\"info\" style=\"margin-top: -3px;\" onclick='vm.updateFinished(" + mkStorePromEntity.getPromId() + ")'>立即结束</i-button>";
                }
                if(mkStorePromEntity.getIsFinished().equalsIgnoreCase(Dict.isFinished.item_2.getItem())){
                    promInfo = promInfo + "&nbsp;&nbsp;&nbsp;&nbsp;<i-button type=\"info\" style=\"margin-top: -3px;\" onclick='vm.updateStarted(" + mkStorePromEntity.getPromId() + ")'>立即开始</i-button>";
                }*/
                promPicUrl = mkStorePromEntity.getListPicUrl();
            }
            mkStorePromEntity.setPromStat(promStat);
            mkStorePromEntity.setPromInfo(promInfo);
            mkStorePromEntity.setPromPicUrl(promPicUrl);
        }
        return mkStorePromList;
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return mkStorePromDao.queryTotal(map);
    }

    @Transactional
    @Override
    public int save(MkStorePromEntity mkStoreProm) {
        if(StringUtils.isNotEmpty(String.valueOf(mkStoreProm.getStoreTopicId()))){
            StoreTopicEntity storeTopicEntity = storeTopicDao.queryObject(mkStoreProm.getStoreTopicId());
            if(storeTopicEntity != null && storeTopicEntity.getIsValid().equalsIgnoreCase(Dict.isValid.item_0.getItem())){
                mkStoreProm.setStoreTopicId(storeTopicEntity.getId());
                MkStorePromTypeEntity typeEntity = mkStorePromTypeDao.queryObject(storeTopicEntity.getPromTypeId());
                if(typeEntity != null){
                    mkStoreProm.setPromTypeId(typeEntity.getPromTypeId());
                }else{
                    throw new RRException("门店活动类型信息不存在 ");
                }
            }else{
                throw new RRException("门店活动信息不存在或已失效");
            }
        }

        MkStorePromEntity promEntity = mkStorePromDao.queryObjectByName(mkStoreProm.getPromName());
        if (promEntity != null) {
            throw new RRException("渠道推广名称已经存在，不能重复");
        }
        SysUserEntity user = ShiroUtils.getUserEntity();
        mkStoreProm.setIsValid("0");
        mkStoreProm.setCreateTime(new Date());
        mkStoreProm.setModTime(new Date());
        mkStoreProm.setCreaterSn(user.getUsername());
        mkStoreProm.setModerSn(user.getUsername());
        //开始时间大于当前时间，则结束状态为未开始
        if(mkStoreProm.getPromBegTime().getTime() > new Date().getTime()){
            mkStoreProm.setIsFinished(Dict.isFinished.item_2.getItem());
        }
        if(mkStoreProm.getPromBegTime().getTime() < new Date().getTime()){
            mkStoreProm.setIsFinished(Dict.isFinished.item_1.getItem());
        }
        return mkStorePromDao.save(mkStoreProm);
    }

    @Transactional
    @Override
    public int update(MkStorePromEntity mkStoreProm) {
        if(StringUtils.isNotEmpty(String.valueOf(mkStoreProm.getStoreTopicId()))){
            StoreTopicEntity storeTopicEntity = storeTopicDao.queryObject(mkStoreProm.getStoreTopicId());
            if(storeTopicEntity != null && storeTopicEntity.getIsValid().equalsIgnoreCase(Dict.isValid.item_0.getItem())){
                mkStoreProm.setStoreTopicId(storeTopicEntity.getId());
                MkStorePromTypeEntity typeEntity = mkStorePromTypeDao.queryObject(storeTopicEntity.getPromTypeId());
                if(typeEntity != null){
                    mkStoreProm.setPromTypeId(typeEntity.getPromTypeId());
                }else{
                    throw new RRException("门店活动类型信息不存在");
                }
            }else{
                throw new RRException("门店活动信息不存在或已失效 ");
            }
        }

        MkStorePromEntity promEntity = mkStorePromDao.queryObjectByName(mkStoreProm.getPromName());
        if (promEntity != null && !mkStoreProm.getPromId().equals(promEntity.getPromId())) {
            throw new RRException("渠道推广名称已经存在，不能重复");
        }
        SysUserEntity user = ShiroUtils.getUserEntity();
        mkStoreProm.setIsValid("0");
        mkStoreProm.setCreateTime(new Date());
        mkStoreProm.setModTime(new Date());
        mkStoreProm.setCreaterSn(user.getUsername());
        mkStoreProm.setModerSn(user.getUsername());
        //开始时间大于当前时间，则结束状态为未开始
        if(mkStoreProm.getPromBegTime().getTime() > new Date().getTime()){
            mkStoreProm.setIsFinished(Dict.isFinished.item_2.getItem());
        }
        if(mkStoreProm.getPromBegTime().getTime() < new Date().getTime()){
            mkStoreProm.setIsFinished(Dict.isFinished.item_1.getItem());
        }
        return mkStorePromDao.update(mkStoreProm);
    }

    @Override
    public int delete(Integer promId) {
        return mkStorePromDao.delete(promId);
    }

    @Transactional
    @Override
    public int deleteBatch(Integer[] promIds) {
        for (int i=0; i<promIds.length; i++){
            MkStorePromEntity prom = new MkStorePromEntity();
            prom.setPromId(promIds[i]);
            prom.setIsValid(Dict.isValid.item_1.getItem());
            mkStorePromDao.update(prom);
        }
        return 1;
//        return mkStorePromDao.deleteBatch(promIds);
    }

    @Transactional
    @Override
    public int updateStartedOrFinished(MkStorePromEntity mkStoreProm) {
        return mkStorePromDao.update(mkStoreProm);
    }
}
