package com.hfkj.service.discount.impl;

import com.hfkj.common.utils.RedisUtil;
import com.hfkj.dao.BsDiscountPkMapper;
import com.hfkj.entity.BsDiscountPk;
import com.hfkj.entity.BsDiscountPkExample;
import com.hfkj.entity.BsDiscountPkRel;
import com.hfkj.service.discount.BsDiscountPkRelService;
import com.hfkj.service.discount.BsDiscountPkService;
import com.hfkj.sysenum.discount.DiscountPkStatusEnum;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @className: BsDiscountPkServiceImpl
 * @author: HuRui
 * @date: 2024/9/2
 **/
@Service("discountPkService")
public class BsDiscountPkServiceImpl implements BsDiscountPkService {
    @Resource
    private BsDiscountPkMapper discountPkMapper;
    @Resource
    private BsDiscountPkRelService discountPkRelService;
    @Resource
    private RedisUtil redisUtil;
    private static final String CACHE_KEY = "DISCOUNT_PK";

    @Override
    public void editData(BsDiscountPk data) {
        data.setUpdateTime(new Date());
        if (data.getId() == null) {
            data.setCreateTime(new Date());
            data.setSaleNum(0);
            data.setStatus(DiscountPkStatusEnum.status1.getCode());
            discountPkMapper.insert(data);

            data.setDiscountPkNo("DPK"+(10000+data.getId()));
            discountPkMapper.updateByPrimaryKey(data);
        } else {
            discountPkMapper.updateByPrimaryKey(data);
        }
        // 缓存
        redisUtil.hset(CACHE_KEY, data.getDiscountPkNo(), data);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public void edit(BsDiscountPk discountPk, List<BsDiscountPkRel> pkRelList) {
        if (discountPk.getId() != null) {
            // 删除关系
            discountPkRelService.delete(discountPk.getDiscountPkNo());
        }
        editData(discountPk);
        for (BsDiscountPkRel rel : pkRelList) {
            rel.setDiscountPkNo(discountPk.getDiscountPkNo());
            discountPkRelService.editData(rel);
        }
    }

    @Override
    public BsDiscountPk getDetail(String discountPkNo) {
        Object cacheObj = redisUtil.hget(CACHE_KEY, discountPkNo);
        if (cacheObj != null) {
            return (BsDiscountPk)cacheObj;
        }
        BsDiscountPkExample example = new BsDiscountPkExample();
        example.createCriteria().andDiscountPkNoEqualTo(discountPkNo).andStatusNotEqualTo(0);
        List<BsDiscountPk> list = discountPkMapper.selectByExample(example);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<BsDiscountPk> getList(Map<String, Object> param) {
        BsDiscountPkExample example = new BsDiscountPkExample();
        BsDiscountPkExample.Criteria criteria = example.createCriteria()
                                                .andStatusNotEqualTo(0);

        if (StringUtils.isNotBlank(MapUtils.getString(param, "discountPkName"))) {
            criteria.andDiscountPkNameLike("%"+MapUtils.getString(param, "discountPkName")+"%");
        }

        if (MapUtils.getInteger(param, "status") != null) {
            criteria.andStatusEqualTo(MapUtils.getInteger(param, "status") );
        }

        if (MapUtils.getInteger(param, "createUserType") != null) {
            criteria.andCreateUserTypeEqualTo(MapUtils.getInteger(param, "createUserType"));
        }

        if (MapUtils.getInteger(param, "createUserId") != null) {
            criteria.andCreateUserIdEqualTo(MapUtils.getLong(param, "createUserId") );
        }

        example.setOrderByClause("create_time desc");
        return discountPkMapper.selectByExample(example);
    }


}
