package com.goafanti.customer.controller;

import com.goafanti.common.bo.Result;
import com.goafanti.common.controller.BaseApiController;
import com.goafanti.common.utils.excel.NewExcelUtil;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.customer.bo.*;
import com.goafanti.customer.service.UserClueService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/admin/userClue")
public class UserClueApiController extends BaseApiController{

	@Resource
	private UserClueService userClueService;
	@Value(value = "${upload.path}")
	private String			uploadPath	= null;


	/**
	 * 客户线索列表
	 * @param in
	 * @return
	 */
	@GetMapping("/list")
	public Result<Pagination<OutUserClueList>> list(InputUserClueList in) {
		return new Result(userClueService.list(in));
	}

	/**
	 * 导入客户线索
	 * @return
	 */
	@PostMapping("/import")
	public Result importUserClue( @RequestParam(value = "file", required = false) MultipartFile file) {
		NewExcelUtil<OutUserClueExcel> excelUtil=new NewExcelUtil<>(OutUserClueExcel.class);
		List<OutUserClueExcel> list =null;
        try {
            list=excelUtil.importExcel(file.getInputStream(),1);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
		Result res = new Result();
		return res.data(userClueService.pushImportUserClue(list));
	}

	/**
	 * 导出客户线索模版
	 * @return
	 */
	@GetMapping("/exportTemplate")
	public Result exportList() {
		return new Result("/tmp/userClue_template.xlsx");
	}

	/**
	 * 新增客户线索
	 * @return
	 */
	@PostMapping("/add")
	public Result add( InputUserClueBo in) {
		Result res = new Result();
		return res.data(userClueService.add(in));
	}


	/**
	 * 线索客户转移
	 * @param in type 0=转交， 1退回线索客户，2=移除线索客户，3=领取线索客户
	 * @return
	 */
	@PostMapping("/transfer")
	public Result transfer( InputUserClueTransferBo in) {
		Result res = new Result();
		if (in.getType()==0&&in.getTransferId()==null){
			res.getError().add(buildError("转移编号不能为空"));
			return res;
		}
		return res.data(userClueService.updateTransfer(in));
	}


}
