package com.hzs.system.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.enums.EDelFlag;
import com.hzs.common.domain.system.base.BdProductStorehouse;
import com.hzs.common.domain.system.base.BdProductStorehouseArea;
import com.hzs.common.domain.system.base.ext.BdProductStorehouseExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.base.mapper.BdProductStorehouseMapper;
import com.hzs.system.base.service.IBdProductStorehouseAreaService;
import com.hzs.system.base.service.IBdProductStorehouseService;
import com.hzs.system.base.vo.BdProductStorehouseVO;
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;

/**
 * 产品绑定仓库表 服务实现类
 */
@Service
public class BdProductStorehouseServiceImpl extends ServiceImpl<BdProductStorehouseMapper, BdProductStorehouse> implements IBdProductStorehouseService {

    @Autowired
    private IBdProductStorehouseAreaService iBdProductStorehouseAreaService;

    @Override
    public List<BdProductStorehouseVO> getProductStorehouseList(BdProductStorehouseVO productStorehouseVO) {
        return baseMapper.getProductStorehouseList(productStorehouseVO);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean insertProductStorehouse(BdProductStorehouseVO productStorehouseVO) {
        BdProductStorehouse bps = BeanUtil.copyProperties(productStorehouseVO, BdProductStorehouse.class);
        baseMapper.insert(bps);
        if (CollectionUtil.isNotEmpty(productStorehouseVO.getProvinceList())) {
            //新增产品仓库绑定地区
            for (Integer i : productStorehouseVO.getProvinceList()) {
                BdProductStorehouseArea bpsa = new BdProductStorehouseArea();
                bpsa.setPkProductStorehouse(bps.getPkId());
                bpsa.setProvince(i);
                bpsa.setCreationTime(productStorehouseVO.getCreationTime());
                bpsa.setPkCountry(productStorehouseVO.getPkCountry());
                bpsa.setPkCreator(productStorehouseVO.getPkCreator());
                iBdProductStorehouseAreaService.save(bpsa);
            }
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean upProductStorehouseVO(BdProductStorehouseVO productStorehouseVO) {
        BdProductStorehouse bps = BeanUtil.copyProperties(productStorehouseVO, BdProductStorehouse.class);
        baseMapper.updateById(bps);
        if (CollectionUtil.isNotEmpty(productStorehouseVO.getProvinceList())) {
            //删除之前绑定的地区
            LambdaUpdateWrapper<BdProductStorehouseArea> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(BdProductStorehouseArea::getDelFlag, EDelFlag.DELETE.getValue());
            updateWrapper.set(BdProductStorehouseArea::getPkModified, productStorehouseVO.getPkCreator());
            updateWrapper.set(BdProductStorehouseArea::getModifiedTime, productStorehouseVO.getCreationTime());
            updateWrapper.eq(BdProductStorehouseArea::getPkProductStorehouse, bps.getPkId());
            iBdProductStorehouseAreaService.update(updateWrapper);
            //新增产品仓库绑定地区
            for (Integer i : productStorehouseVO.getProvinceList()) {
                BdProductStorehouseArea bpsa = new BdProductStorehouseArea();
                bpsa.setPkProductStorehouse(bps.getPkId());
                bpsa.setProvince(i);
                bpsa.setPkCreator(productStorehouseVO.getPkCreator());
                bpsa.setCreationTime(productStorehouseVO.getCreationTime());
                bpsa.setPkCountry(bps.getPkCountry());
                iBdProductStorehouseAreaService.save(bpsa);
            }
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delBdStorehouse(Long pkId) {
        Date date = new Date();
        LambdaUpdateWrapper<BdProductStorehouse> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(BdProductStorehouse::getPkId, pkId);
        updateWrapper.set(BdProductStorehouse::getDelFlag, EDelFlag.DELETE.getValue());
        updateWrapper.set(BdProductStorehouse::getPkModified, SecurityUtils.getUserId());
        updateWrapper.set(BdProductStorehouse::getModifiedTime, date);
        baseMapper.update(null, updateWrapper);
        //删除绑定的地区
        LambdaUpdateWrapper<BdProductStorehouseArea> updateWrapper1 = new LambdaUpdateWrapper<>();
        updateWrapper1.set(BdProductStorehouseArea::getDelFlag, EDelFlag.DELETE.getValue());
        updateWrapper1.set(BdProductStorehouseArea::getPkModified, SecurityUtils.getUserId());
        updateWrapper1.set(BdProductStorehouseArea::getModifiedTime, date);
        updateWrapper1.eq(BdProductStorehouseArea::getPkProductStorehouse, pkId);
        iBdProductStorehouseAreaService.update(updateWrapper1);
        return true;
    }

    @Override
    public List<BdProductStorehouseExt> queryBdProductStorehouseByCondition(List<Integer> productId, Integer province) {
        return baseMapper.queryBdProductStorehouseByCondition(productId, province);
    }
}
