package com.th.demo.service.impl.develop;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.th.demo.model.develop.ExcelImport;
import com.th.demo.tools.ClassTool;
import com.th.demo.tools.ExcelTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;

import com.th.demo.mapping.develop.DevImexportMapper;
import com.th.demo.model.develop.DevImexport;
import com.th.demo.service.develop.DevImexportService;

import com.th.demo.model.system.Type;
import com.th.demo.tools.Tools;
import org.springframework.util.MultiValueMap;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;

@Service("DevImexportService")
public class DevImexportServiceImpl implements DevImexportService {
    @Autowired
    DevImexportMapper devImexportMapper;


    String result = Type.FAIL;
    int num = 0;

    @Override
    public String query(int page, int rows) {
        PageHelper.startPage(page, rows);
        List<DevImexport> list = devImexportMapper.select();
        PageInfo<DevImexport> pageInfo = new PageInfo<>(list);
        result = JSON.toJSONString(pageInfo);
        return result;
    }


    @Override
    public String save(
            String id,
            String name,
            String param1,
            String param2,
            String param3,
            String param4,
            String userId
    ) {
        DevImexport devImexport;
        try {
            if (id == null || id.equals("")) {
                devImexport = new DevImexport();

                devImexport.setId(id);
                devImexport.setName(name);
                devImexport.setParam1(param1);
                devImexport.setParam2(param2);
                devImexport.setParam3(param3);
                devImexport.setParam4(param4);

                num = devImexportMapper.insert(devImexport);
                result = Tools.moreThanZero(num);
            } else {
                devImexport = devImexportMapper.selectByPrimaryKey(id);
                devImexport.setId(id);
                devImexport.setName(name);
                devImexport.setParam1(param1);
                devImexport.setParam2(param2);
                devImexport.setParam3(param3);
                devImexport.setParam4(param4);

                num = devImexportMapper.updateByPrimaryKey(devImexport);
                result = Tools.moreThanZero(num);
            }
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            return result;
        }
    }

    @Override
    public String remove(String id, String userId) {
        try {
            num = 0;
            DevImexport devImexport = devImexportMapper.selectByPrimaryKey(id);

            num = devImexportMapper.updateByPrimaryKey(devImexport);
            result = Tools.moreThanZero(num);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            return result;
        }
    }

    @Override
    public String importExcel(HttpServletRequest request) throws IOException {
        num = 0;
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultiValueMap<String, MultipartFile> files = multipartRequest.getMultiFileMap();
        List<ExcelImport> list = ExcelTools.readExcel( files.get("file").get(0), ExcelImport.class);
        for (int i = 0; i < list.size(); i++) {
            DevImexport devImexport = new DevImexport();
            devImexport.setName(list.get(i).getName());
            devImexport.setParam1(list.get(i).getParam1());
            devImexport.setParam2(list.get(i).getParam2());
            devImexport.setParam3(list.get(i).getParam3());
            devImexport.setParam4(list.get(i).getParam4());
            num += devImexportMapper.insert(devImexport);
        }
        result = Tools.moreThanZero(num);
        return result;
    }

    @Override
    public List<Map<String, Object>> exportExcel(String[] arrKey) {

        List<DevImexport> listDevImexport = devImexportMapper.select();
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        for (DevImexport devImexport : listDevImexport) {
            Map<String, Object> map = new HashMap<String, Object>();
            for (int i = 0; i < arrKey.length; i++) {
                map.put(arrKey[i], ClassTool.getFieldValueByFieldName(arrKey[i], devImexport));
            }
            list.add(map);
        }

        return list;
    }
}
