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.CargoChangeRecordMapper;
import com.shinsoft.sto.model.main.CargoChangeRecord;
import com.shinsoft.sto.service.main.CargoChangeRecordService;
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 java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CargoChangeRecordServiceImp extends ServiceImpl<CargoChangeRecordMapper, CargoChangeRecord> implements CargoChangeRecordService {

    ResultJSON resultJSON;

    @Autowired
    CargoChangeRecordMapper cargoChangeRecordMapper;

    @Override
    public ResultJSON query(Integer page, Integer rows, Map<String, Object> params) {
        try {
            Page<CargoChangeRecord> recordPage = new Page<>(page, rows);
            Page<CargoChangeRecord> list = cargoChangeRecordMapper.selectCargoChangeRecordPage(recordPage, 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 {
            CargoChangeRecord record = cargoChangeRecordMapper.selectById(id);
            resultJSON = JSONTools.toResultJSON(record);
        } 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();
            CargoChangeRecord record = JSON.parseObject(json, new TypeReference<CargoChangeRecord>() {});

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

            resultJSON = JSONTools.toResultJSON(record);
        } 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();
            CargoChangeRecord record = cargoChangeRecordMapper.selectById(id);
            record.setCancelFlag("1");
            record.setModifyId(userId);
            record.setModifyTime(date);
            cargoChangeRecordMapper.updateById(record);
            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>>() {});
            CargoChangeRecord record;
            for (String id : list) {
                record = cargoChangeRecordMapper.selectById(id);
                record.setCancelFlag("1");
                record.setModifyId(userId);
                record.setModifyTime(date);
                cargoChangeRecordMapper.updateById(record);
            }
            resultJSON = JSONTools.toResultJSON("");
        } catch (Exception ex) {
            ex.printStackTrace();
            resultJSON = JSONTools.toResultJSON(ResponseCodeMsg.CODE_EX, ex);
        }
        return resultJSON;
    }
}