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.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.SystemFieldConstants;
import com.hzs.common.core.enums.EDelFlag;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.domain.system.base.BdStorehouse;
import com.hzs.common.domain.system.base.BdStorehouseAreaMiddle;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.base.mapper.BdStorehouseMapper;
import com.hzs.system.base.service.IBdStorehouseAreaMiddleService;
import com.hzs.system.base.service.IBdStorehouseService;
import com.hzs.system.base.vo.BdStorehouseAreaMiddleVO;
import com.hzs.system.base.vo.BdStorehouseVO;
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;
import java.util.Set;

/**
 * <p>
 * 仓库信息服务实现类
 * </p>
 *
 * @author hzs
 * @since 2022-08-02
 */
@Service
public class BdStorehouseServiceImpl extends ServiceImpl<BdStorehouseMapper, BdStorehouse> implements IBdStorehouseService {


    @Autowired
    private IBdStorehouseAreaMiddleService iBdStorehouseAreaMiddleService;
    /**
     * 查询仓库列表信息
     *
     * @param bdStorehouse
     * @return
     */
    @Override
    public List<BdStorehouseVO> selectBdStorehouseList(BdStorehouseVO bdStorehouse) {
        return baseMapper.selectBdStorehouseList(bdStorehouse);
    }

    /**
     * 新增仓库信息
     *
     * @param bdStorehouseVO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean intBdStorehouse(BdStorehouseVO bdStorehouseVO) {
        BdStorehouse bdStorehouse=  BeanUtil.copyProperties(bdStorehouseVO, BdStorehouse.class);
        baseMapper.insert(bdStorehouse);
        if(CollectionUtil.isNotEmpty(bdStorehouseVO.getProvinceList())){
            BdStorehouseAreaMiddleVO sam=BeanUtil.copyProperties(bdStorehouseVO, BdStorehouseAreaMiddleVO.class);
            sam.setPkStorehouse(bdStorehouse.getPkId());
            iBdStorehouseAreaMiddleService.intStoArMiddle(sam);
        }
        return true;
    }

    /**
     * 查询单条仓库信息
     *
     * @param bdStorehouse
     * @return
     */
    @Override
    public BdStorehouse getBdStorehouse(BdStorehouse bdStorehouse) {
        return baseMapper.getBdStorehouse(bdStorehouse);
    }

    /**
     * 校验仓库信息是否重复
     * @param bdStorehouse
     * @return
     */
    @Override
    public BdStorehouse checkBdStorehouse(BdStorehouse bdStorehouse) {
        return baseMapper.checkBdStorehouse(bdStorehouse);
    }

    @Override
    public List<BdStorehouse> queryStorehouseByIds(Set<Integer> storehouseIds, Integer pkCountry, Integer pkCorp) {
        QueryWrapper<BdStorehouse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SystemFieldConstants.ENABLE_STATE, EYesNo.YES.getIntValue());
        queryWrapper.eq(SystemFieldConstants.PK_COUNTRY, pkCountry);
        queryWrapper.eq(SystemFieldConstants.PK_CORP, pkCorp);
        queryWrapper.in(SystemFieldConstants.PK_ID, storehouseIds);
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 修改保存仓库信息
     *
     * @param bdStorehouse
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateBdStorehouse(BdStorehouseVO bdStorehouse) {
        UpdateWrapper<BdStorehouse> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("PK_ID", bdStorehouse.getPkId());
        updateWrapper.set("CODE", bdStorehouse.getCode());
        updateWrapper.set("NAME", bdStorehouse.getName());
        updateWrapper.set("WARE_ADDRESS", bdStorehouse.getWareAddress());
        updateWrapper.set("PROVINCE", bdStorehouse.getProvince());
        updateWrapper.set("CITY", bdStorehouse.getCity());
        if(bdStorehouse.getCounty() != null) {
            updateWrapper.set("COUNTY", bdStorehouse.getCounty());
        }
        updateWrapper.set("CONTACTS", bdStorehouse.getContacts());
        updateWrapper.set("MOBILE_PHONE", bdStorehouse.getMobilePhone());
        if(bdStorehouse.getPkCorp() != null) {
            updateWrapper.set("PK_CORP", bdStorehouse.getPkCorp());
        }
        updateWrapper.set("PK_MODIFIED", bdStorehouse.getPkModified());
        updateWrapper.set("MODIFIED_TIME",bdStorehouse.getModifiedTime());
        update(updateWrapper);
        BdStorehouseAreaMiddleVO sam=BeanUtil.copyProperties(bdStorehouse, BdStorehouseAreaMiddleVO.class);
        sam.setPkStorehouse(bdStorehouse.getPkId());
        UpdateWrapper<BdStorehouseAreaMiddle> updateWrapper1=new UpdateWrapper<>();
        updateWrapper1.eq("PK_STOREHOUSE",bdStorehouse.getPkId());
        updateWrapper1.set("DEL_FLAG",EDelFlag.DELETE.getValue());
        iBdStorehouseAreaMiddleService.update(updateWrapper1);
        if(CollectionUtil.isNotEmpty(bdStorehouse.getProvinceList())){
            for(Integer i:bdStorehouse.getProvinceList()){
                BdStorehouseAreaMiddle sa=new BdStorehouseAreaMiddle();
                sa.setProvince(i);
                sa.setPkStorehouse(bdStorehouse.getPkId());
                sa.setPkCountry(bdStorehouse.getPkCountry());
                sa.setPkCreator(bdStorehouse.getPkModified());
                sa.setCreationTime(bdStorehouse.getModifiedTime());
                iBdStorehouseAreaMiddleService.save(sa);
            }
        }
        return true;
    }

    /**
     * @description: 删除仓库
     * @author: zhang jing
     * @date: 2023/5/16 16:42
     * @param: [pkId]
     * @return: boolean
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delBdStorehouse(BdStorehouseVO bdStorehouse) {
        UpdateWrapper<BdStorehouse> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("PK_ID", bdStorehouse.getPkId());
        updateWrapper.set("DEL_FLAG", EDelFlag.DELETE.getValue());
        updateWrapper.set("PK_MODIFIED", bdStorehouse.getPkModified());
        updateWrapper.set("MODIFIED_TIME", bdStorehouse.getModifiedTime());
        baseMapper.update(null, updateWrapper);
        UpdateWrapper<BdStorehouseAreaMiddle> updateWrapper1 = new UpdateWrapper<>();
        updateWrapper1.eq("PK_STOREHOUSE", bdStorehouse.getPkId());
        updateWrapper1.set("DEL_FLAG", EDelFlag.DELETE.getValue());
        updateWrapper1.set("PK_MODIFIED", bdStorehouse.getPkModified());
        updateWrapper1.set("MODIFIED_TIME", bdStorehouse.getModifiedTime());
        iBdStorehouseAreaMiddleService.update(updateWrapper1);
        return true;
    }

}
