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

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.th.demo.mapping.develop.DevUpdownMapper;
import com.th.demo.mapping.develop.DevUpdownPathsMapper;
import com.th.demo.model.develop.DevTableProperty;
import com.th.demo.model.develop.DevUpdown;
import com.th.demo.model.develop.DevUpdownPaths;
import com.th.demo.model.system.Type;
import com.th.demo.service.develop.UpDownService;
import com.th.demo.tools.Tools;
import org.apache.poi.openxml4j.opc.PackagePartCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.MultiValueMap;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by dell on 2019-01-07.
 */
@Service("UpDownService")
public class UpDownServiceImpl implements UpDownService {
    @Autowired
    DevUpdownMapper devUpdownMapper;
    @Autowired
    DevUpdownPathsMapper devUpdownPathsMapper;
    int num = 0;
    String result = Type.FAIL;
    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd hh:mm:ss").create();//gson json的三方框架 数据传递格式

    @Transactional
    @Override
    public String upload(HttpServletRequest request) throws IOException {
        String result = Type.FAIL;
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        String param = multipartRequest.getParameter("param");
        MultiValueMap<String, MultipartFile> files = multipartRequest.getMultiFileMap();
        DevUpdown devUpdown = new DevUpdown();
        devUpdown.setParam(param);
        devUpdownMapper.insert(devUpdown);
        String fid = devUpdown.getId();
        for (int i = 0; i < files.get("file").size(); i++) {
            MultipartFile mf = files.get("file").get(i);
            File tempFile = new File("E:\\" + mf.getOriginalFilename());
            mf.transferTo(tempFile);
            DevUpdownPaths devUpdownPaths = new DevUpdownPaths();
            devUpdownPaths.setFid(fid);
            devUpdownPaths.setPaths("E:\\" + mf.getOriginalFilename());
            devUpdownPathsMapper.insert(devUpdownPaths);
        }
        result = Type.SUCCESS;
        return result;


    }

    @Override
    public String query(int page, int rows) {
        PageHelper.startPage(page, rows);
        List<DevUpdown> list = new ArrayList<DevUpdown>();
        list = devUpdownMapper.select();
        //查询当前分页数据
        PageInfo<DevUpdown> pageInfo = new PageInfo<DevUpdown>(list);
        result = gson.toJson(pageInfo);
        return result;
    }

    @Override
    public String remove(String id, String userId) {
        num = 0;

        List<DevUpdownPaths> list = new ArrayList<DevUpdownPaths>();
        list = devUpdownPathsMapper.selectByFid(id);
        if (list.size()>0) {
            for (DevUpdownPaths paths : list) {
                String path = paths.getPaths();
                File file = new File(path);
                file.delete();
            }
        }
        num += devUpdownPathsMapper.deleteByFid(id);
        num += devUpdownMapper.deleteByPrimaryKey(id);
        return Tools.moreThanZero(num);
    }

    @Override
    public String queryPaths(String id) {
        List<DevUpdownPaths> list = devUpdownPathsMapper.selectByFid(id);
        return gson.toJson(list);
    }

    @Override
    public String removePaths(String id, String userId) {
        num = 0;
        DevUpdownPaths devUpdownPaths = devUpdownPathsMapper.selectByPrimaryKey(id);
        String path = devUpdownPaths.getPaths();
        File file = new File(path);
        file.delete();
        devUpdownPaths.setCancelFlag("9");
        devUpdownPaths.setCancelId(userId);
        devUpdownPaths.setCancelTime(new Date());
        num = devUpdownPathsMapper.updateByPrimaryKey(devUpdownPaths);
        return Tools.moreThanZero(num);
    }
}
