package com.goafanti.user.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.goafanti.admin.service.AttachmentService;
import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.CertifyApiController;
import com.goafanti.common.enums.AttachmentType;
import com.goafanti.common.model.Attachment;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.common.utils.excel.NewExcelUtil;
import com.goafanti.user.bo.InputUserChannel;
import com.goafanti.user.bo.OutUserChannel;
import com.goafanti.user.service.UserChannelService;

@RestController
@RequestMapping(value = "/api/user/channel")
public class UserChannelApiController  extends CertifyApiController{

	@Resource
	private UserChannelService 	userChannelService;
	@Resource
	private AttachmentService	attachmentService;


	/**
	 * 	新增渠道
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Result add(InputUserChannel in) {
		Result res = new Result();
		if (StringUtils.isBlank(in.getUserName())) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "用户名称"));
			return  res;
		}
		res.setData(userChannelService.add(in));
		return res;
	}

	/**
	 * 	删除渠道
	 * @return
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public Result delete(String ids) {
		Result res = new Result();
		if (StringUtils.isBlank(ids)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "编号"));
			return  res;
		}
		res.setData(userChannelService.delete(ids));
		return res;
	}


	/**
	 * 	Excel批量导入渠道客户
     * @return
     */
    @RequestMapping(value = "/import" , method = RequestMethod.POST)
    public Result importTemplate(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request) {
    	Result res=new Result();
    	//判断文件是否存在
        if(null == file){
      	  res.getError().add(buildError("文件不存在！","文件不存在！"));
      	  return res;
        }
    	String fileName = file.getOriginalFilename();
    	if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
        	  res.getError().add(buildError("格式不正确","格式不正确"));
        	  return res;
          }

         res.data(userChannelService.batchListReceivables(file));
    	return res;
    }


    /**
	 * 已存在渠道客户列表
	 * @return
	 */
      @RequestMapping(value = "/erorrList" , method = RequestMethod.GET)
      public Result erorrList(Integer pageNo,Integer pageSize)  {
    	  Result res =new Result();
    	  res.data(userChannelService.getErorrList(pageNo,pageSize));
  		 return  res;
      	}


	/**
	 * 导出已存在渠道客户
	 * @param response
	 * @return
	 */
      @RequestMapping(value = "/export" , method = RequestMethod.GET)
      public Result exportReceivables(HttpServletResponse response)  {
    	  @SuppressWarnings("unchecked")
		List<OutUserChannel> list =(List<OutUserChannel>) userChannelService.getErorrList(1,99999).getList();
      	NewExcelUtil<OutUserChannel>excel=new NewExcelUtil<>(OutUserChannel.class);
  		 return  excel.exportExcel(list,"未导入渠道客户表",response);
      	}

      /**
	   * 下载回款批量导入Excel模板
       * @param response
       * @return
       */
      @RequestMapping(value = "/downloadTemplate", method = RequestMethod.GET)
      public Result downloadTemplateFile(HttpServletResponse response, String sign) {
          Result res = new Result();
          AttachmentType attachmentType = AttachmentType.getField(sign);
          if (attachmentType == AttachmentType.USER_CHANNEL) {
              String fileName = "";
              Attachment af = attachmentService.selectByReceivbles(sign);
              if (null == af) {
                  res.getError().add(buildError(ErrorConstants.FILE_NON_EXISTENT, "", "找不到文件！"));
              } else {
                  String path = af.getFilePath();
                  String suffix = path.substring(path.lastIndexOf("."));
                  fileName = AttachmentType.USER_CHANNEL.getDesc() + suffix;
                  downloadFile(response, fileName, path);
              }
          } else {
              res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
          }
          return res;
      }




}
