package com.hzs.sale.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EDelFlag;
import com.hzs.common.core.enums.ETransactionKey;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.domain.sale.product.BdAreaClassify;
import com.hzs.common.domain.sale.product.BdAreaClassifySpecialArea;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.sale.product.controller.manage.params.AreaClassifyParam;
import com.hzs.sale.product.mapper.BdAreaClassifyMapper;
import com.hzs.sale.product.service.IBdAreaClassifyService;
import com.hzs.sale.product.service.IBdAreaClassifySpecialAreaService;
import com.hzs.sale.product.vo.AreaClassifyVo;
import com.hzs.system.base.ITransactionServiceApi;
import com.hzs.system.sys.dto.LoginUser;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 商品分类 服务实现类
 */
@Service
public class BdAreaClassifyServiceImpl extends ServiceImpl<BdAreaClassifyMapper, BdAreaClassify> implements IBdAreaClassifyService {

    @Autowired
    private IBdAreaClassifyService iBdAreaClassifyService;
    @Autowired
    private IBdAreaClassifySpecialAreaService iBdAreaClassifySpecialAreaService;
    @DubboReference
    ITransactionServiceApi iTransactionServiceApi;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveAreaClassify(AreaClassifyParam areaClassifyParam, LoginUser loginUser) {
        BdAreaClassify areaClassify = BeanUtil.copyProperties(areaClassifyParam, BdAreaClassify.class);
        areaClassify.setPkCreator(loginUser.getUserId());
        areaClassify.setCreationTime(new Date());
        areaClassify.setPkCountry(loginUser.getDataCountry());
        R<Integer> transaction = iTransactionServiceApi.createTransaction(loginUser.getDataCountry(), ETransactionKey.SPEC, areaClassify.getClassifyName(), EYesNo.NO, EYesNo.NO);
        areaClassify.setPkTransactionKey(transaction.getData());
        baseMapper.insert(areaClassify);
        List<BdAreaClassifySpecialArea> list = new ArrayList<>();
        if (areaClassifyParam.getSpecialAreaList() != null) {
            for (Integer number : areaClassifyParam.getSpecialAreaList()) {
                BdAreaClassifySpecialArea areaClassifySpecialArea = new BdAreaClassifySpecialArea();
                areaClassifySpecialArea.setSpecialArea(number);
                areaClassifySpecialArea.setPkAreaClassify(areaClassify.getPkId());
                areaClassifySpecialArea.setPkCreator(loginUser.getUserId());
                areaClassifySpecialArea.setCreationTime(new Date());
                areaClassifySpecialArea.setPkCountry(loginUser.getDataCountry());
                list.add(areaClassifySpecialArea);
            }
        }

        if (list.size() > 0) {
            iBdAreaClassifySpecialAreaService.saveBatch(list);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAreaClassify(AreaClassifyParam areaClassifyParam, LoginUser loginUser) {
        BdAreaClassify areaClassify = BeanUtil.copyProperties(areaClassifyParam, BdAreaClassify.class);
        areaClassify.setPkModified(loginUser.getUserId());
        areaClassify.setModifiedTime(new Date());
        R<Integer> transaction = iTransactionServiceApi.createTransaction(loginUser.getDataCountry(), ETransactionKey.SPEC, areaClassify.getClassifyName(), EYesNo.NO, EYesNo.NO);
        areaClassify.setPkTransactionKey(transaction.getData());
        baseMapper.updateById(areaClassify);
        // 清空 中间表数据重新添加
        iBdAreaClassifySpecialAreaService.deleteByAreaClassifyPk(areaClassify.getPkId());
        List<BdAreaClassifySpecialArea> list = new ArrayList<>();
        if (areaClassifyParam.getSpecialAreaList() != null) {
            for (Integer number : areaClassifyParam.getSpecialAreaList()) {
                BdAreaClassifySpecialArea areaClassifySpecialArea = new BdAreaClassifySpecialArea();
                areaClassifySpecialArea.setSpecialArea(number);
                areaClassifySpecialArea.setPkAreaClassify(areaClassify.getPkId());
                areaClassifySpecialArea.setPkCreator(loginUser.getUserId());
                areaClassifySpecialArea.setCreationTime(new Date());
                areaClassifySpecialArea.setPkCountry(loginUser.getDataCountry());
                list.add(areaClassifySpecialArea);
            }
        }
        if (list.size() > 0) {
            iBdAreaClassifySpecialAreaService.saveBatch(list);
        }
    }

    @Override
    public BdAreaClassify getAreaClassify(Integer areaClassifyId) {
        return baseMapper.selectById(areaClassifyId);
    }

    @Override
    public List<AreaClassifyVo> selectByAreaClassifyList(AreaClassifyParam areaClassifyParam) {
        return baseMapper.selectByAreaClassifyList(areaClassifyParam);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAreaClassify(Integer pkId) {
        LambdaUpdateWrapper<BdAreaClassify> bdAreaClassify = new LambdaUpdateWrapper<>();
        bdAreaClassify.eq(BdAreaClassify::getPkId, pkId);
        bdAreaClassify.set(BdAreaClassify::getPkModified, SecurityUtils.getUserId());
        bdAreaClassify.set(BdAreaClassify::getModifiedTime, new Date());
        bdAreaClassify.set(BdAreaClassify::getDelFlag, EDelFlag.DELETE.getValue());
        iBdAreaClassifyService.update(bdAreaClassify);

        // 清空 中间表数据重新添加
        iBdAreaClassifySpecialAreaService.deleteByAreaClassifyPk(pkId);
    }

}
