package com.shinsoft.sto.service.impl;

import com.shinsoft.sto.model.Reverse;
import com.shinsoft.sto.mapper.ReverseMapper;
import com.shinsoft.sto.service.ReverseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;


import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.shinsoft.tools.JSONTools;
import com.shinsoft.tools.model.common.ResultJSON;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

/**
 * <p>
 * 倒库记录表 服务实现类
 * </p>
 *
 * @author system
 * @since 2024-12-24
 */
@Service
public class ReverseServiceImp extends ServiceImpl<ReverseMapper, Reverse> implements ReverseService {


    ResultJSON resultJSON;

    @Autowired
    ReverseMapper reverseMapper;

    @Override
    public ResultJSON query(int page, int rows, String params) {
        JSONObject jsonObject = JSON.parseObject(params);
        Page<Reverse> reversePage = new Page<>(page, rows);
        QueryWrapper<Reverse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cancel_flag", "0");
        queryWrapper.orderByDesc("add_time");
        String storeNm = "";
        String areaCd = "";
        String stackCd = "";
        String itemNm = "";
        if (ObjectUtils.isNotEmpty(jsonObject.get("storeNm"))) {
            storeNm = jsonObject.getString("storeNm");
            queryWrapper.like("store_nm", storeNm);
        }
        if (ObjectUtils.isNotEmpty(jsonObject.get("areaCd"))) {
            areaCd = jsonObject.getString("areaCd");
            queryWrapper.like("area_cd", areaCd);
        }
        if (ObjectUtils.isNotEmpty(jsonObject.get("stackCd"))) {
            stackCd = jsonObject.getString("stackCd");
            queryWrapper.like("stack_cd", stackCd);
        }
        if (ObjectUtils.isNotEmpty(jsonObject.get("itemNm"))) {
            itemNm = jsonObject.getString("itemNm");
            queryWrapper.like("item_nm", itemNm);
        }
        if (ObjectUtils.isNotEmpty(jsonObject.get("date"))) {
            JSONArray jsonArray = jsonObject.getJSONArray("date");
            if (!CollectionUtils.isEmpty(jsonArray)) {
                Date startDate = null;
                Date endDate = null;
                try {
                    startDate = new SimpleDateFormat("yyyy-MM-dd").parse(jsonArray.getString(0));
                    endDate = new SimpleDateFormat("yyyy-MM-dd").parse(jsonArray.getString(1));
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
                queryWrapper.between("REVERSE_DATE", startDate,endDate);
            }
        }
        IPage<Reverse> list = reverseMapper.selectPage(reversePage, queryWrapper);
        resultJSON = JSONTools.toResultJSON(list);
        return resultJSON;
    }

    @Override
    public ResultJSON queryByPK(String id) {
        Reverse reverse = reverseMapper.selectByPK(id);
        resultJSON = JSONTools.toResultJSON(reverse);
        return resultJSON;
    }


    @Override
    public ResultJSON save(String userId, String json) {
        Date date = new Date();
        Reverse reverse = JSON.parseObject(json, new TypeReference<Reverse>() {
        });
        if (StringUtils.isEmpty(reverse.getId())) {
            reverse.setCancelFlag("0");
            reverse.setAddId(userId);
            reverse.setAddTime(date);
            reverseMapper.insert(reverse);
        } else {
            reverse.setModifyId(userId);
            reverse.setModifyTime(date);
            reverseMapper.updateById(reverse);
        }

        resultJSON = JSONTools.toResultJSON(reverse);
        return resultJSON;
    }

    @Override
    public ResultJSON remove(String userId, String id) {
        Date date = new Date();
        Reverse reverse = reverseMapper.selectById(id);
        reverse.setCancelFlag("1");
        reverse.setCancelId(userId);
        reverse.setCancelTime(date);
        reverseMapper.updateById(reverse);
        resultJSON = JSONTools.toResultJSON("");
        return resultJSON;
    }

    @Override
    public ResultJSON removeBatch(String userId, String ids) {
        Date date = new Date();
        List<String> list = JSON.parseObject(ids, new TypeReference<List<String>>() {
        });
        Reverse reverse;
        for (String id : list) {
            reverse = reverseMapper.selectById(id);
            reverse.setCancelFlag("1");
            reverse.setCancelId(userId);
            reverse.setCancelTime(date);
            reverseMapper.updateById(reverse);
        }
        resultJSON = JSONTools.toResultJSON("");
        return resultJSON;
    }

}
