package com.jkcredit.invoice.controller.business;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.com.taiji.sdk.model.comm.protocol.eoms.company.B2bCompanyModel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jkcredit.invoice.annotation.LoginRequired;
import com.jkcredit.invoice.annotation.annotationdes.AuthenticationInterceptor;
import com.jkcredit.invoice.model.entity.customer.Customer;
import com.jkcredit.invoice.model.entity.CustomerRecharge;
import com.jkcredit.invoice.model.entity.customer.CustomerRec;
import com.jkcredit.invoice.model.entity.manager.Param;
import com.jkcredit.invoice.service.customer.CustomerRecService;
import com.jkcredit.invoice.service.customer.CustomerRechargeService;
import com.jkcredit.invoice.service.customer.CustomerService;
import com.jkcredit.invoice.service.lowerservice.CustomeLowerService;
import com.jkcredit.invoice.service.manager.ParamService;
import com.jkcredit.invoice.util.DateUtil;
import com.jkcredit.invoice.util.ExportUtil;
import com.jkcredit.invoice.util.RespR;
import com.jkcredit.invoice.util.WordUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.util.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.*;

/**
 * 客户操作
 */
@Api(tags = "客户操作")
@RestController
@RequestMapping(value = {"/customer"})
/**
* @Description 客户操作
* @Author  mashengyi
* @Date   2022/2/8 18:16
* @Param
* @Return
* @Exception
*
*/
@Slf4j
public class CustomerController {
    @Autowired
    CustomerService customerService;
    @Autowired
    CustomerRechargeService customerRechargeService;

    @Autowired
    CustomeLowerService lowerService;

    @Autowired
    CustomerRecService customerRecService;


    @Autowired
    private ParamService paramService;
    /**
     * 分页查询客户
     *
     * @param page    参数集
     * @return 用户集合
     */
    @PostMapping("/findCustomer")
    @ApiOperation(value="分页查询客户", notes="分页查询客户")
    @LoginRequired(role = AuthenticationInterceptor.AUTH_ADMIN)
    public RespR getCustomersByPage(Page page, Customer customer) {

        return new RespR(customerService.findAllCustomers(page, customer));
    }

    /**
     * 分页查询客户
     *
     * @param page    参数集
     * @return 用户集合
     */
    @PostMapping("/findCustomerRecharge")
    @ApiOperation(value="分页查询客户充值信息", notes="分页查询客户充值信息")
    @LoginRequired(role = AuthenticationInterceptor.AUTH_ADMIN)
    public RespR findCustomerRecharge(Page page, CustomerRecharge customerRecharge) {

        return new RespR(customerRechargeService.findAllCustomerRecharge(page, customerRecharge));
    }



    /**
     * 分页查询客户
     *
     * @param page    参数集
     * @return 用户集合
     */
    @PostMapping("/findCustomerRechargeMoney")
    @ApiOperation(value="分页查询客户端客户充值信息", notes="分页查询客户端客户充值信息")
    public RespR findCustomerRechargeMoney(Page page, CustomerRecharge customerRecharge) {

        return new RespR(customerRechargeService.findAllCustomerRechargeMoney(page, customerRecharge));
    }
    /**
     * 导出客户信息
     * @param
     * @return 用户集合
     */
    @GetMapping("/findCustomerRecListExport")
    @ApiOperation(value="客户信息导出", notes="客户信息导出")
    @LoginRequired(role = AuthenticationInterceptor.AUTH_ADMIN)
    public void findCustomerRechargeExport(String  customerName,String companyName,String companyBelongName,HttpServletResponse response) throws Exception{
        CustomerRec customerRec = new CustomerRec();
        customerRec.setCustomerName(customerName);
        customerRec.setCompanyBelongName(companyBelongName);
        customerRec.setCompanyName(companyName);
        List<CustomerRec> customerRecs = customerRecService.findAllCustomerRecForExport(customerRec);

        if(customerRecs!=null && customerRecs.size()>0){
            customerRecs.stream().forEach(customerRec1 -> {

                if("2".equals(customerRec1.getBussinessType())){
                    customerRec1.setBussinessType("无车");
                }else if("0".equals(customerRec1.getBussinessType())){
                    customerRec1.setBussinessType("自有车");
                }
            });
        }
        ExportParams exportParams = new ExportParams("客户信息","客户信息");
        ExportUtil.exportExcel(customerRecs,CustomerRec.class,"客户信息导出",exportParams,response);
    }
    /**
     * 分页查询客户注册信息
     *
     * @param page    参数集
     * @return 用户集合
     */
    @PostMapping("/findCustomerRecList")
    @ApiOperation(value="分页查询客户注册信息", notes="分页查询客户注册信息")
    @LoginRequired
    public RespR findCustomerRecList(Page page, CustomerRec customerRec) {

        return new RespR(customerRecService.findAllCustomerRec(page, customerRec));
    }
    /**
     * 添加客户
     * @return 用户集合
     */
    @PostMapping("/addCustomer")
    @ApiOperation(value="添加客户", notes="添加客户")
    @LoginRequired
    public RespR addCustomer(@RequestBody  Customer customer) {
        return new RespR(customerService.addCustomers(customer),"客户添加失败，请查看是否重复");
    }
    /**
     * 修改客户状态
     * @return 用户集合
     */
    @PostMapping("/updateCustomer")
    @ApiOperation(value="更新用户", notes="更新用户")
    @LoginRequired(role = AuthenticationInterceptor.AUTH_ADMIN)
    public RespR updateCustomer(@RequestBody  Customer customer) {

        return new RespR(customerService.updateCustomer(customer));
    }
    /**
     * 修改客户状态
     * @return 用户集合
     */
    @PostMapping("/customeRecStop")
    @ApiOperation(value="停用备案用户", notes="停用备案用户")
    @LoginRequired(role = AuthenticationInterceptor.AUTH_ADMIN)
    public RespR customeRecStop(@RequestBody  CustomerRec customerRec) {
        customerRec.setRecStatus(4);
        return customerService.updateCustomerRecStatus(customerRec);
    }
    /**
     * 修改客户状态
     * @return 用户集合customerRecAdd
     */
    @PostMapping("/customerRecAdd")
    @ApiOperation(value="手工添加备案信息", notes="手工添加备案信息")
    @LoginRequired(role = AuthenticationInterceptor.AUTH_ADMIN)
    public RespR customerRecAdd(@RequestBody  CustomerRec customerRec) {
        customerRec.setInterType(1);
        List<CustomerRec> customerRecList = new ArrayList<>();
        customerRecList.add(customerRec);
        return lowerService.customeRec(customerRecList);
    }

    /**
     * 修改客户状态
     * @return 用户集合customerRecAdd
     */
    @PostMapping("/customeRecQueryUpper")
    @ApiOperation(value="查询备案信息", notes="手工添加备案信息")
    @LoginRequired
    public RespR customeRecQueryUpper(@RequestBody  CustomerRec customerRec) {
        RespR<List<B2bCompanyModel>> respR = customerService.customerRecQuery(customerRec);

        if(respR.getCode() == 1){
            return respR;
        }else{
            List<B2bCompanyModel> resArr = respR.getData();
            if(resArr == null || resArr.size() == 0){
                return new RespR(false,"未注册，无法回填");
            }else {

                if(resArr.size()>=1){
                    resArr.forEach(b2bCompanyModel->{
                        if(b2bCompanyModel.getCompanyType().equals(customerRec.getCompanyType())){
                            customerRec.setCompanyType(b2bCompanyModel.getCompanyType());
                            customerRec.setOperatingRangeType(b2bCompanyModel.getOperatingRangeType());
                            customerRec.setCompanyLeader(b2bCompanyModel.getContact());
                            customerRec.setCompanyLeaderPhone(b2bCompanyModel.getEmergencyTel());
                            customerRec.setCompanyAdress(b2bCompanyModel.getBuyerAddr());
                            customerRec.setCompanyPhone(b2bCompanyModel.getBuyerTel());
                            customerRec.setCompanyOpenbank(b2bCompanyModel.getBuyerBank());
                            customerRec.setCompanyOpenbankAcc(b2bCompanyModel.getBuyerBankAccount());
                            customerRec.setCompanyPhone(b2bCompanyModel.getTel());

                        }
                    });
                }

                return new RespR(customerRec);
            }
        }


    }


    /**
     * 充值
     * @return 用户集合
     */
    @PostMapping("/customRecharge")
    @ApiOperation(value="账号充值", notes="账号充值")
    @LoginRequired(role = AuthenticationInterceptor.AUTH_ADMIN)
    public RespR customRecharge(@RequestBody CustomerRecharge customerRecharge) {

        return new RespR(customerService.customRecharge(customerRecharge));
    }

    @PostMapping("/customeRec")
    @ApiOperation(value="用户备案确认", notes="用户备案确认")
    @LoginRequired(role = AuthenticationInterceptor.AUTH_ADMIN)
    public RespR customeRec(@RequestBody CustomerRec customerRec){
        return customerService.customeRec(customerRec);
    }

    @PostMapping("/contractAdd")
    @ApiOperation(value="协议上传确认", notes="协议上传确认")
    @LoginRequired(role = AuthenticationInterceptor.AUTH_ADMIN)
    public RespR contractAdd(@RequestBody CustomerRec customerRec){
        return customerService.contractAdd(customerRec);
    }
    @GetMapping("/contractDownload")
    @ApiOperation(value="协议下载", notes="协议下载")
    public RespR contractDownload(String customerRecId,HttpServletResponse response){
        CustomerRec customerRec = (CustomerRec) customerService.contractDownload(customerRecId).getData();
        String fileName = customerRec.getLowerFileName();
        InputStream inputStream = null;
        OutputStream outputStream = null;
        if(customerRec.getLowerBase64Str()!=null){
            try {
             //将base64编码的字符串解码成字节数组
                byte[] byteArray = new sun.misc.BASE64Decoder().decodeBuffer(customerRec.getLowerBase64Str());
                inputStream = new ByteArrayInputStream(byteArray);
                response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(fileName, "UTF-8"));
                outputStream = response.getOutputStream();
                IOUtils.copy(inputStream, outputStream);
                response.flushBuffer();
                return new RespR(true);
            } catch (IOException e) {
                log.error("协议下载CustomerController.contractDownload协议下载失败{}",e.getMessage());
            } finally {
                IOUtils.closeQuietly(inputStream);
                IOUtils.closeQuietly(outputStream);
            }
        }

        return new RespR(false);
    }
    @GetMapping("/generateWord")
    @ApiOperation(value="文档下载", notes="文档下载")
    public RespR generateWord(String customerRecId,HttpServletResponse response){
        CustomerRec customerRec = (CustomerRec) customerService.generateWordDownload(customerRecId).getData();
        if(null == customerRec || null ==customerRec.getServiceStartTime() || null ==customerRec.getServiceEndTime()){
            return new RespR(false,"企业维护信息缺失服务时间，请手动维护");
        }
        String fileName = "合作说明.doc";
        Map<String, String> dataMap = new HashMap<>();
        dataMap.put("a", customerRec.getCompanyBelongName());
        dataMap.put("b", customerRec.getCompanyName());
        dataMap.put("c", customerRec.getCompanyReferencenum());
        dataMap.put("d", customerRec.getCompanyAdress());
        dataMap.put("e", customerRec.getCompanyPhone());
        dataMap.put("f", customerRec.getCompanyOpenbank());
        dataMap.put("g", customerRec.getCompanyOpenbankAcc());
        dataMap.put("j", customerRec.getServiceStartTime().substring(0,10));
        dataMap.put("k", customerRec.getServiceEndTime().substring(0,10));
        Calendar now = Calendar.getInstance();
        dataMap.put("l",now.get(Calendar.YEAR)+"");
        dataMap.put("h",(now.get(Calendar.MONTH) + 1) + "");
        dataMap.put("i", now.get(Calendar.DAY_OF_MONTH)+"");
        OutputStream outputStream =null;
        try{
            response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(fileName, "UTF-8"));
            outputStream = response.getOutputStream();
            WordUtil.ExportSimpleWord(dataMap,"/static/templates/excel/",outputStream);
            response.flushBuffer();
        }catch (Exception e){
            log.error("文档下载CustomerController.generateWord文档下载失败{}",e.getMessage());
        }finally {
            IOUtils.closeQuietly(outputStream);
        }
        return new RespR(false);
    }




    /**
     * 分页查询客户备案预警信息
     *
     * @param page    参数集
     * @return 用户集合
     */
    @PostMapping("/findCustomerRecTimeList")
    @ApiOperation(value="客户备案预警查询", notes="客户备案预警查询")
    @LoginRequired(role = AuthenticationInterceptor.AUTH_ADMIN)
    public RespR findCustomerRecTimeList(Page page, CustomerRec customerRec) {
        IPage ipage = null;
        if((StringUtils.isNotEmpty(customerRec.getServiceEndTime())  && !"null".equals(customerRec.getServiceEndTime()))||
                (StringUtils.isNotEmpty(customerRec.getCompanyName())  && !"null".equals(customerRec.getCompanyName()))||
        (StringUtils.isNotEmpty(customerRec.getCompanyBelongName())  && !"null".equals(customerRec.getCompanyBelongName()))){
            if(StringUtils.isNotEmpty(customerRec.getServiceEndTime())  && !"null".equals(customerRec.getServiceEndTime())){
            String [] res =customerRec.getServiceEndTime().split(",");
            customerRec.setServiceEndTimeStart(res[0]);
            customerRec.setServiceEndTimeEnd(res[1].replace("00:00:00","23:59:59"));
            }
            ipage = customerRecService.findAllCustomerRecWarning(page, customerRec);
        }else{
            Param param = paramService.getParamsByParamName("CUST_EARLY_WARNING");
            ipage = customerRecService.findAllCustomerRecWarning(page, customerRec);
            List<CustomerRec> lists = ipage.getRecords();
            if(lists.size()>0){
                //遍历删除
                Iterator<CustomerRec> iterator = lists.iterator();
                while (iterator.hasNext()) {
                    CustomerRec customerRec1 = iterator.next();
                    if (((DateUtil.daysBetween(customerRec1.getServiceEndTime(),new Date()) < 0 ) )
                            || (Integer.valueOf(param.getParamValue()) <= (DateUtil.daysBetween(customerRec1.getServiceEndTime(),new Date())))) {
                        iterator.remove();//使用迭代器的删除方法删除
                    }
                }
            }
            ipage.setTotal(lists.size());
        }

        return new RespR(ipage);
    }





    /**
     * 分页查询余额告警用户
     *
     * @param page    参数集
     * @return 用户集合
     */
    @PostMapping("/findCustomerMoney")
    @ApiOperation(value="分页查询余额告警用户", notes="分页查询余额告警用户")
    @LoginRequired(role = AuthenticationInterceptor.AUTH_ADMIN)
    public RespR getCustomerMoneysByPage(Page page, Customer customer) {
        Param param = paramService.getParamsByParamName("CUST_MONEY_WARNING");
        IPage ipage = null;
        if(customer.getMoneyUpper() == null || customer.getMoneyUpper() == -1){
            customer.setMoneyUpper(Double.valueOf(param.getParamValue()));
            ipage = customerService.findAllNomoneyCustomers(page, customer);
        }else if(customer.getMoneyUpper() == null || customer.getMoneyUpper() == 0){
            ipage = customerService.findAllNomoneyCustomersByZero(page, customer);
        }else {
            ipage = customerService.findAllNomoneyCustomers(page, customer);
        }

        return new RespR(ipage);
    }
    /**
     * @param customerRec
     * @return
     */
    @PostMapping("/customeRecQueryListByPage")
    @ApiOperation(value="用户备案列表分页查询", notes="用户备案列表分页查询")
    @LoginRequired
    public RespR customeRecQueryListByPage(Page page, CustomerRec customerRec){
        IPage ipage = customerRecService.findAllCustomerRec(page, customerRec);
        return new RespR(ipage);
    }

}
