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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shinsoft.sto.mapper.main.PriceMapper;
import com.shinsoft.sto.model.main.Price;
import com.shinsoft.sto.service.main.PriceService;
import com.shinsoft.tools.JSONTools;
import com.shinsoft.tools.model.common.ResponseCodeMsg;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 价格管理 服务实现类
 * </p>
 *
 * @author system
 * @since 2024-10-15
 */
@Service
public class PriceServiceImp extends ServiceImpl<PriceMapper, Price> implements PriceService {

    ResultJSON resultJSON;

    @Autowired
    PriceMapper priceMapper;

    @Override
    public ResultJSON query(int page, int rows, Map<String, Object> params) {
        try {
            Page<Price> pricePage = new Page<>(page, rows);
            Page<Price> list = priceMapper.selectPricePage(pricePage, params);
            resultJSON = JSONTools.toResultJSON(list);
        } catch (Exception ex) {
            ex.printStackTrace();
            resultJSON = JSONTools.toResultJSON(ResponseCodeMsg.CODE_EX, ex);
        }
        return resultJSON;
    }

    @Override
    public ResultJSON queryByPK(String id) {
        try {
            Price price = priceMapper.selectById(id);
            resultJSON = JSONTools.toResultJSON(price);
        } catch (Exception ex) {
            ex.printStackTrace();
            resultJSON = JSONTools.toResultJSON(ResponseCodeMsg.CODE_EX, ex);
        }
        return resultJSON;
    }

    @Override
    public ResultJSON save(String userId, String json) {
        try {
            Date date = new Date();
            Price price = JSON.parseObject(json, new TypeReference<Price>() {});

            if (StringUtils.isEmpty(price.getId())) {
                price.setCancelFlag("0");
                price.setAddId(userId);
                price.setAddTime(date);
                priceMapper.insert(price);
            } else {
                price.setModifyId(userId);
                price.setModifyTime(date);
                priceMapper.updateById(price);
            }

            resultJSON = JSONTools.toResultJSON(price);
        } catch (Exception ex) {
            ex.printStackTrace();
            resultJSON = JSONTools.toResultJSON(ResponseCodeMsg.CODE_EX, ex);
        }
        return resultJSON;
    }

    @Override
    public ResultJSON remove(String userId, String id) {
        try {
            Date date = new Date();
            Price price = priceMapper.selectById(id);
            price.setCancelFlag("1");
            price.setModifyId(userId);
            price.setModifyTime(date);
            priceMapper.updateById(price);
            resultJSON = JSONTools.toResultJSON("");
        } catch (Exception ex) {
            ex.printStackTrace();
            resultJSON = JSONTools.toResultJSON(ResponseCodeMsg.CODE_EX, ex);
        }
        return resultJSON;
    }

    @Override
    public ResultJSON removeBatch(String userId, String ids) {
        try {
            Date date = new Date();
            List<String> list = JSON.parseObject(ids, new TypeReference<List<String>>() {});
            Price price;
            for (String id : list) {
                price = priceMapper.selectById(id);
                price.setCancelFlag("1");
                price.setModifyId(userId);
                price.setModifyTime(date);
                priceMapper.updateById(price);
            }
            resultJSON = JSONTools.toResultJSON("");
        } catch (Exception ex) {
            ex.printStackTrace();
            resultJSON = JSONTools.toResultJSON(ResponseCodeMsg.CODE_EX, ex);
        }
        return resultJSON;
    }
}