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