package com.shinsoft.sto.service.impl.main;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shinsoft.sto.mapper.main.WareMapper;
import com.shinsoft.sto.model.main.Ware;
import com.shinsoft.sto.service.main.WareService;
import com.shinsoft.tools.JSONTools;
import com.shinsoft.tools.model.common.ResultJSON;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

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

/**
 * <p>
 * 仓库 服务实现类
 * </p>
 *
 * @author system
 * @since 2024-10-15
 */
@Service
public class WareServiceImp extends ServiceImpl<WareMapper, Ware> implements WareService {

    ResultJSON resultJSON;

    @Autowired
    WareMapper wareMapper;

    @Override
    public ResultJSON query(int page, int rows,  Map<String, Object> map) {
        Page<Ware> warePage = new Page<>(page, rows);
        QueryWrapper<Ware> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cancel_flag", "0");
        queryWrapper.orderByDesc("add_time");
        
        // 解析查询参数
        if (!ObjectUtils.isEmpty(map)) {
            if (!ObjectUtils.isEmpty(map.get("wareNm"))) {
                queryWrapper.like("ware_nm", map.get("wareNm"));
            }
            if (!ObjectUtils.isEmpty(map.get("wareCode"))) {
                queryWrapper.like("ware_code", map.get("wareCode"));
            }
        }
        
        Page<Ware> list = wareMapper.selectPage(warePage, queryWrapper);
        resultJSON = JSONTools.toResultJSON(list);
        return resultJSON;
    }

    @Override
    public ResultJSON queryByPK(String id) {
        Ware ware = wareMapper.selectById(id);
        resultJSON = JSONTools.toResultJSON(ware);
        return resultJSON;
    }

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

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

    @Override
    public ResultJSON remove(String userId, String id) {
        Date date = new Date();
        Ware ware = wareMapper.selectById(id);
        ware.setCancelFlag("1");
        ware.setModifyId(userId);
        ware.setModifyTime(date);
        wareMapper.updateById(ware);
        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>>() {
        });
        Ware ware;
        for (String id : list) {
            ware = wareMapper.selectById(id);
            ware.setCancelFlag("1");
            ware.setModifyId(userId);
            ware.setModifyTime(date);
            wareMapper.updateById(ware);
        }
        resultJSON = JSONTools.toResultJSON("");
        return resultJSON;
    }
}

