package com.winsun.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.winsun.auth.core.annotion.Permission;
import com.winsun.auth.core.base.controller.BaseController;
import com.winsun.auth.core.common.model.ResponseData;
import com.winsun.bean.RhPhone;
import com.winsun.mapper.RhPhoneMapper;
import com.winsun.utils.ImportExcel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * @Author: chenhaojian
 * @Date: 2021/1/14
 */
@RestController
@RequestMapping("/rhPhone") //
public class RhPhoneController extends BaseController {

    private static RhPhoneMapper rhPhoneMapper;

    /**
     * 导入
     */
    private static final int HEADER_NUM = -1;
    private static final int SHEET_INDEX = 0;

    @Autowired
    public RhPhoneController(RhPhoneMapper rhPhoneMapper){
        RhPhoneController.rhPhoneMapper = rhPhoneMapper;
    }


    @Permission(menuname = "查询信息", value = "list", method = RequestMethod.POST)
    public Map<String,Object> dataList(@RequestParam(name = "id",required = false)String id,
                                       @RequestParam(name = "phone",required = false)String phone,
                                       @RequestParam(name = "tcName",required = false)String tcName,
                                       @RequestParam(name = "pageNo", required = false) int pageNo,
                                       @RequestParam(name = "pageSize", required = false) int pageSize
    ){
        Map<String,Object> map = new HashMap<>();
        Wrapper<RhPhone> wrapper = new EntityWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id",id);
        wrapper.eq(StringUtils.isNotBlank(phone), "phone",phone);
        wrapper.eq(StringUtils.isNotBlank(tcName), "tcName",tcName);

        Page<RhPhone> page = new Page<>(pageNo, pageSize);
        List<RhPhone> aPackage = rhPhoneMapper.selectPage(page, wrapper);
        page.setRecords(aPackage);
        map.put("page",ResponseData.success(page,"查询成功！"));
        return map;
    }

    @RequestMapping(value = "addData",method = RequestMethod.POST)
    public ResponseData<String> addData(@RequestParam(name = "phone") String phone){
        Wrapper<RhPhone> wrapper = new EntityWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(phone), "phone",phone);
        List<RhPhone> list = rhPhoneMapper.selectList(wrapper);
        if(list != null && list.size()>0){
            return ResponseData.error("融合号码已存在！");
        }
        RhPhone rhPhone = new RhPhone();
        rhPhone.setPhone(phone);
        try {
            rhPhoneMapper.insert(rhPhone);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseData.success();
    }

    @RequestMapping(value = "deleteData",method = RequestMethod.POST)
    public ResponseData<String> deleteData(@RequestParam(name = "id") String id){
        if(StringUtils.isBlank(id)){
            return ResponseData.error("");
        };
        Wrapper<RhPhone> wrapper = new EntityWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id",id);
        try {
            rhPhoneMapper.delete(wrapper);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseData.success();
    }



    /**
     * 导入融合号码数据
     * @return
     */

    @PostMapping(value = "importAccNbr")
    public ResponseData<String> importAccNbr(@RequestParam(name = "file") MultipartFile file){
        StringBuffer error = new StringBuffer();
        if (!StringUtils.endsWithAny(file.getOriginalFilename(), "xlsx", "xls")) {
            return ResponseData.error("手工上传数据仅支持Excel文件，其他格式不支持！");
        }
        if ( (double)file.getSize()/1048576 > 100) {
            return ResponseData.error("文件过大，无法上传");
        }

        ImportExcel importExcel = null;
        try {
            importExcel = new ImportExcel(file, HEADER_NUM, SHEET_INDEX);
        } catch (Exception e) {
            return ResponseData.error("导入异常！");
        }


        List<Map<String, Object>> listMap = importExcel.getDataListMap();
        System.out.println(listMap);
        if (listMap.size() == 1) {
            return ResponseData.error("文件不能为空");
        }
        //检查excel表中是否有重复号码
        int size = listMap.size();
        Set<String> accountSet = new HashSet<>(size);
        for (int i = 0; i < size; i++) {
            Map<String, Object> temp = listMap.get(i);
            accountSet.add((String) temp.get("a"));
        }
        if (accountSet.size() != size) {
            return ResponseData.error("文件中有重复的订单编号，请确认订单编号不能重复!");
        }
        try {
            for (int i = 0; i < listMap.size(); i++) {
                RhPhone rhPhone=new RhPhone();
                Map<String, Object> map = listMap.get(i);
                rhPhone.setPhone(map.get("a").toString());
                rhPhoneMapper.insert(rhPhone);
            }
            return ResponseData.success(error.toString(), "导入完成");
        }catch (Exception e){
            e.printStackTrace();
            return ResponseData.error("导入异常！");
        }

    }

}
