package com.sunda.spmswms.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmswms.entity.InventoryCheckActionDtl;
import com.sunda.spmswms.mapper.InventoryCheckActionDtlMapper;
import com.sunda.spmswms.service.IInventoryCheckActionDtlService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 盘盈盘亏明细表 服务实现类
 * </p>
 *
 * @author Wayne
 * @since 2021-05-10
 */
@Service
public class InventoryCheckActionDtlServiceImpl extends ServiceImpl<InventoryCheckActionDtlMapper, InventoryCheckActionDtl> implements IInventoryCheckActionDtlService {
    /*获取InventoryCheckAction行项*/
    @Override
    public ResponseResult getInvCheckAcDtl(String uuid) {
        if (StringUtils.isEmpty(uuid)) {
            return ResponseResult.error("uuid is mandatory required");
        }
        try {
            return ResponseResult.success().add("invCheckAcDtls", this.baseMapper.selectInvDtl(uuid));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error("failed when query Inventory Check Action Dtl");
        }
    }

    /*更新InventoryCheckAcDtl行项
     * 以uuid及taskRowId为key，存在则更新，不存在则新建
     * */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult updateInvCheckDtl(String uuid, JSONArray invCheckAcDtls) {
        try {
            if (StringUtils.isEmpty(uuid)) {
                throw new Exception("uuid is mandatory required");
            }

            for (int i = 0; i < invCheckAcDtls.size(); i++) {
                InventoryCheckActionDtl inventoryCheckActionDtl = invCheckAcDtls.getObject(i, InventoryCheckActionDtl.class);
                QueryWrapper<InventoryCheckActionDtl> invAcDtlQuerywarp = new QueryWrapper<InventoryCheckActionDtl>().eq("UUID", uuid).eq("TASK_ROW_ID", inventoryCheckActionDtl.getTaskRowId());
                List<InventoryCheckActionDtl> invAcDtls = this.baseMapper.selectList(invAcDtlQuerywarp);
                if (invAcDtls.size() > 0) {
                    // 存在则更新
                    this.baseMapper.update(inventoryCheckActionDtl, invAcDtlQuerywarp);
                } else {
                    // 不存在，新建
                    if (StringUtils.isEmpty(inventoryCheckActionDtl.getTaskRowId())) {
                        int taskRowId = this.baseMapper.selectMaxTaskRowId(uuid);
                        taskRowId += 10;
                        inventoryCheckActionDtl.setTaskRowId(String.valueOf(taskRowId));
                    }
                    inventoryCheckActionDtl.setUuid(uuid);
                    this.baseMapper.insert(inventoryCheckActionDtl);
                }
            }
            return ResponseResult.success().add("invCheckAcDtls", this.baseMapper.selectList(new QueryWrapper<InventoryCheckActionDtl>().eq("UUID", uuid)));
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseResult.error(e.getMessage());
        }
    }

    @Override
    public List<InventoryCheckActionDtl> getICADList(String uuid) {
        return this.baseMapper.selectList(new QueryWrapper<InventoryCheckActionDtl>().eq("UUID", uuid));
    }

    @Override
    public List<Map<String, Object>> getInventoryActionDtlM(String uuid) {
        return this.baseMapper.getInventoryActionDtlM(uuid);
    }

    @Override
    public List<Map<String, Object>> getInventoryActionDtlX(String uuid) {
        return this.baseMapper.getInventoryActionDtlX(uuid);
    }
}
