package com.ruoyi.ibs.grid.controller;


import com.github.pagehelper.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataPageInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ibs.grid.domain.entity.IbsAnchorAddress;
import com.ruoyi.ibs.grid.domain.excel.NewPersonAnchor;
import com.ruoyi.ibs.grid.domain.excel.NewQyAnchor;
import com.ruoyi.ibs.grid.domain.vo.*;
import com.ruoyi.ibs.grid.service.AnchorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


@Api(tags = "锚点相关")
@RestController
@RequestMapping("/ibs/anchor/")
public class AnchorController extends BaseController {

    @Autowired
    AnchorService service;

    @Log(title = "地址管理-查询锚点列表")
    @ApiOperation(value = "查询锚点列表")
    @GetMapping("List")
    public TableDataPageInfo<AnchorVo> selectAnchorList(@RequestParam String userid, @RequestParam String custType, String anchorName,String street,String community,String source,String custStatus,String custAddressStatus,String status) {
        Page<Object> page = startPage();
        List<AnchorVo> list = service.selectAnchorList(userid, custType, anchorName,street,community,source,custStatus,custAddressStatus,status);
        return getDataTable(list, page);
    }
    @Log(title = "地址管理-查询审核页列表")
    @ApiOperation(value = "查询审核页列表")
    @GetMapping("AuditList")
    @ResponseBody
    public TableDataPageInfo<AuditVo> selectAuditList(@RequestParam String userid, @RequestParam String custType,String custStatus,String custAddressStatus) {
        Page<Object> page = startPage();
        List<AuditVo> list = service.selectAuditList(userid, custType,custStatus,custAddressStatus);
        return getDataTable(list,page);

    }
    @Log(title = "地址管理-查询已忽略地址列表")
    @ApiOperation(value = "查询已忽略地址列表")
    @GetMapping("IgnoreList")
    public TableDataPageInfo<IgnoreVo> selectIgnoreList(@RequestParam String userid, @RequestParam String custType, @RequestParam int pageNum,String custStatus,String custAddressStatus) {
        Page<Object> page = startPage();
        List<IgnoreVo> list = service.selectIgnoreList(userid, custType,custStatus,custAddressStatus);
        return getDataTable(list,page);


    }
    @Log(title = "地址管理-已忽略地址删除")
    @ApiOperation(value = "已忽略地址删除")
    @PostMapping("DelIgnore")
    public AjaxResult DelIgnore(@RequestBody int[] ids) {
        return service.delIgnore(ids);
    }

    @Log(title = "地址管理-锚点删除")
    @ApiOperation(value = "锚点删除")
    @PostMapping("DelAnchor")
    public AjaxResult DelAnchor(@RequestBody int[] ids) {
        return service.delAnchor(ids);
    }


    @Log(title = "地址管理-锚点详情")
    @ApiOperation(value = "锚点详情")
    @GetMapping("Detail")
    public AjaxResult AnchorDetail(String anchorid) {
        return service.anchorDetai(anchorid);
    }

    @Log(title = "地址管理-已忽略地址重新审核")
    @ApiOperation(value = "已忽略地址重新审核")
    @PostMapping("ReAudit")
    public AjaxResult reAudit(@RequestBody int[] ids) {
        return service.reAudit(ids);
    }

    @Log(title = "需审核地址忽略")
    @ApiOperation(value = "需审核地址忽略")
    @PostMapping("SetIgnore")
    public AjaxResult setIgnore(@RequestBody int[] ids) {
        return service.setIgnore(ids);
    }

    @Log(title = "地址管理-需审核地址忽略2.0")
    @ApiOperation(value = "需审核地址忽略2.0")
    @PostMapping("SetIgnoreUp")
    public AjaxResult setIgnoreUp(@RequestBody List<AuditVo> list) {
        return service.setIgnoreUp(list);
    }

    @Log(title = "地址管理-锚点编辑")
    @ApiOperation(value = "锚点编辑")
    @PostMapping("UpdateAnchor")
    public AjaxResult updateAnchor(@RequestBody AnchorDetail anchorDetail, String userid) {
        return service.updateAnchor(anchorDetail, userid);
    }

    @Log(title = "地址管理-地址通过审核2.0")
    @ApiOperation(value = "地址通过审核2.0(更新地址分级版)")
    @PostMapping("AuditAddressUp")
    public AjaxResult AuditAddressUp(@RequestBody List<AuditVo> list) {
        return service.passAudit2(list);
    }

    @Log(title = "地址管理-导出模板")
    @ApiOperation(value = "导出模板", produces = "application/octet-stream")
    @GetMapping("exportModel")
    public void export(HttpServletResponse response) {
        try {
            List<NewAnchor> list = new ArrayList<>();
            ExcelUtil<NewAnchor> util = new ExcelUtil<>(NewAnchor.class);
            util.exportExcel(response, list, "sheet1");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Log(title = "地址管理-导出企业/商户模板")
    @ApiOperation(value = "导出企业/商户模板", produces = "application/octet-stream")
    @GetMapping("exportQyModel")
    public void exportQy(HttpServletResponse response) {
        try {
            List<NewQyAnchor> list = new ArrayList<>();
            ExcelUtil<NewQyAnchor> util = new ExcelUtil<>(NewQyAnchor.class);
            util.exportExcel(response, list, "sheet1");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Log(title = "地址管理-导出个人锚点模板")
    @ApiOperation(value = "导出个人锚点模板", produces = "application/octet-stream")
    @GetMapping("exportPersonModel")
    public void exportGr(HttpServletResponse response) {
        try {
            List<NewPersonAnchor> list = new ArrayList<>();
            ExcelUtil<NewPersonAnchor> util = new ExcelUtil<>(NewPersonAnchor.class);
            util.exportExcel(response, list, "个人模板");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Log(title = "地址管理-分支流程-锚点添加")
    @ApiOperation("分支流程-锚点添加")
    @PostMapping("addAnchor")
    public AjaxResult addAnchor(@RequestBody List<NewAnchor> list) {
        return service.insertNewAnchor(list);
    }

    @Log(title = "地址管理-企业/商户批量导入")
    @ApiOperation("企业/商户批量导入")
    @PostMapping("importQyModel")
    public AjaxResult exportQy(@RequestBody List<NewQyAnchor> list) {
            return service.insertNewQyAnchor(list, "0");
    }

    @Log(title = "地址管理-个人锚点批量导入")
    @ApiOperation("个人锚点批量导入")
    @PostMapping("importGrModel")
    public AjaxResult exportGr(@RequestBody List<NewPersonAnchor> list) {
            return service.insertNewGrAnchor(list);
    }

    @Log(title = "地址管理-刷新提示")
    @ApiOperation("刷新提示")
    @PostMapping("refresh")
    public AjaxResult refresh(String userid, String custType) {
        return service.refresh(userid, custType);
    }

    @Log(title = "地址管理-导出个人失败客户")
    @ApiOperation(value = "导出个人失败客户", produces = "application/octet-stream")
    @PostMapping("exportFailGr")
    public void exportFailGr(HttpServletResponse response,@RequestBody List<NewPersonAnchor> list) {
        try {
            ExcelUtil<NewPersonAnchor> util = new ExcelUtil<>(NewPersonAnchor.class);
            util.exportExcel(response, list, "sheet1");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Log(title = "地址管理-导出商户/企业失败客户")
    @ApiOperation(value = "导出商户/企业失败客户", produces = "application/octet-stream")
    @PostMapping("exportFailQy")
    public void exportFailQy(HttpServletResponse response,@RequestBody List<NewQyAnchor> list) {
        try {
            ExcelUtil<NewQyAnchor> util = new ExcelUtil<>(NewQyAnchor.class);
            util.exportExcel(response, list, "sheet1");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Log(title = "地址管理-企业/商户批量反显")
    @ApiOperation("企业/商户批量反显")
    @PostMapping("returnQyModel")
    public AjaxResult importQy1(@RequestPart(value = "file") MultipartFile file) {
        try {
            ExcelUtil<NewQyAnchor> util = new ExcelUtil<>(NewQyAnchor.class);
            List<NewQyAnchor> list = util.importExcel(file.getInputStream());
            return AjaxResult.success(list);
        } catch (IOException e) {
            return AjaxResult.error("导入失败，请检查模板以及数据准确性");
        }
    }

    @Log(title = "地址管理-个人锚点批量反显")
    @ApiOperation("个人锚点批量反显")
    @PostMapping("returnGrModel")
    public AjaxResult importGr1(@RequestPart(value = "file") MultipartFile file) {
        try {
            ExcelUtil<NewPersonAnchor> util = new ExcelUtil<>(NewPersonAnchor.class);
            List<NewPersonAnchor> list = util.importExcel(file.getInputStream());
            return AjaxResult.success(list);
        } catch (IOException e) {
            return AjaxResult.error("导入失败，请检查模板以及数据准确性");
        }
    }

    @Log(title = "地址管理-初始化地址")
    @ApiOperation("初始化地址")
    @GetMapping("init")
    public AjaxResult selectAddress(long id) {
       return service.selectAddress(id);
    }

    @Log(title = "地址管理-获取对应客户表id")
    @ApiOperation("获取对应客户表id")
    @GetMapping("getCust")
    public AjaxResult getCust(long id) {
        return AjaxResult.success(service.getCust(id));
    }



}
