package com.sunda.spmsoversea.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsoversea.dto.WhsDumpConfigDTO;
import com.sunda.spmsoversea.dto.WhsDumpConfigQueryDTO;
import com.sunda.spmsoversea.entity.WhsDumpConfig;
import com.sunda.spmsoversea.mapper.WhsDumpConfigMapper;
import com.sunda.spmsoversea.service.IWhsDumpConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 * IPO工厂转储配置表 服务实现类
 * </p>
 *
 * @author Hzc
 * @since 2023-02-22
 */
@Service
public class WhsDumpConfigServiceImpl extends ServiceImpl<WhsDumpConfigMapper, WhsDumpConfig> implements IWhsDumpConfigService {

    @Override
    public IPage getWhsDumpConfigPage(WhsDumpConfigQueryDTO whsDumpConfigQueryDTO) {
        IPage iPage = whsDumpConfigQueryDTO.getPage();
        List<Map<String, Object>> mapList = this.baseMapper.getWhsDumpConfigPage(iPage, whsDumpConfigQueryDTO);
        iPage.setRecords(mapList);
        return iPage;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult createOrUpdateWhsDumpConfig(WhsDumpConfigDTO whsDumpConfigDTO, String userId) throws Exception {
        WhsDumpConfig whsDumpConfig = new WhsDumpConfig();
        whsDumpConfig.setWorkCode(userId);
        whsDumpConfig.setOutWerks(whsDumpConfigDTO.getOutWerks());
        whsDumpConfig.setInWerks(whsDumpConfigDTO.getInWerks());
        whsDumpConfig.setReceiveWerks(whsDumpConfigDTO.getReceiveWerks());
        if(whsDumpConfigDTO.getUuid()!=null && whsDumpConfigDTO.getUuid().length()>0){
            whsDumpConfig.setUuid(whsDumpConfigDTO.getUuid());
            QueryWrapper<WhsDumpConfig> queryWrapper = new QueryWrapper<WhsDumpConfig>().eq("UUID", whsDumpConfigDTO.getUuid());
            whsDumpConfig.setUpdateDate(new Date());
            this.baseMapper.update(whsDumpConfig, queryWrapper);
        }else{
            String uuid  = UUID.randomUUID().toString().replaceAll("-","").toLowerCase();
            whsDumpConfig.setUuid(uuid);
            whsDumpConfig.setSpmsStatus("1");
            whsDumpConfig.setCreateDate(new Date());
            this.baseMapper.insert(whsDumpConfig);
        }
        return ResponseResult.success("操作成功！");
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult deleteWhsDumpConfig(WhsDumpConfigDTO whsDumpConfigDTO, String userId) throws Exception {
        if(whsDumpConfigDTO.getUuid()==null){
            throw new Exception("UUID不能为空" );
        }
        WhsDumpConfig whsDumpConfig = new WhsDumpConfig();
        whsDumpConfig.setWorkCode(userId);
        whsDumpConfig.setUuid(whsDumpConfigDTO.getUuid());
        whsDumpConfig.setSpmsStatus("0");
        QueryWrapper<WhsDumpConfig> queryWrapper = new QueryWrapper<WhsDumpConfig>().eq("UUID", whsDumpConfigDTO.getUuid());
        whsDumpConfig.setUpdateDate(new Date());
        this.baseMapper.update(whsDumpConfig, queryWrapper);

        return ResponseResult.success("操作成功！");
    }
}
