package common.controller;

import com.google.common.collect.ImmutableList;
import common.model.*;
import common.service.ContractService;
import common.service.MenuService;
import dic.OperateObjectTypeEnum;
import exception.TipException;
import org.apache.commons.io.IOUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import security.annotation.CurrentAccount;
import util.NewUserLogThread;
import util.ResultModel;
import util.UserLogThread;
import util.ValidateUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;

/**
 * Created by mxq on 17/12/21.
 * 优先测试账号查询
 */
@Controller
@RequestMapping("{platform}/contract")
public class ContractController {

    @Autowired
    private ContractService service;


    @RequestMapping(value = "find", method = RequestMethod.GET)
    @ResponseBody
    public ResultModel findAll(@CurrentAccount User loginAccount, @PathVariable String platform,
                               @RequestParam String startDate, @RequestParam String endDate) {
        return ResultModel.OK(service.findAll(startDate, endDate, platform));
    }

    @RequestMapping(value = "export", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<byte[]> export(@PathVariable String platform, @RequestParam String startDate
                                , @RequestParam String endDate, HttpServletRequest request, HttpServletResponse response) {
        List<Contract> contractList = service.findAll(startDate, endDate, platform);

        InputStream in = null;
        byte[] content = new byte[0];
        StringBuilder sb = new StringBuilder();
        sb.append("时间,合同编号,客户账号,签约方,销售,开始日期,结束日期,套餐类型,合同金额,折扣,收款金额,合同状态");
        if(ValidateUtil.isValid(contractList)){
            for (Contract c : contractList){
                sb.append(c.getDs()).append(c.getContractCode()).append(c.getEmail()).append(c.getCustomerBody()).append(c.getSaleName())
                        .append(c.getStartDate()).append(c.getEndDate()).append(c.getPriceLevelName()).append(c.getMoney()).append(c.getPayMoney())
                        .append(c.getStatus());
            }
        }
        content = sb.toString().getBytes();
        HttpHeaders headers = new HttpHeaders();
        response.setHeader("filename", "contraact.xls");
        response.setHeader("content-disposition", "attachment; filename=contraact.xls");
        response.setHeader("Content-Type", "application/vnd.ms-excel");

        //返回文件字符串
        return new ResponseEntity<byte[]>(content, headers, HttpStatus.CREATED);
    }

    @RequestMapping(value = "findone", method = RequestMethod.GET)
    @ResponseBody
    public ResultModel findOne(@CurrentAccount User loginAccount, @RequestParam String code) {
        return ResultModel.OK(service.findOne(code));
    }

    @RequestMapping(value = "checkAccount", method = RequestMethod.GET)
    @ResponseBody
    public ResultModel checkAccount(@CurrentAccount User loginAccount, @RequestParam String email) {
        return ResultModel.OK(service.checkAccount(email));
    }

    @RequestMapping(value = "find/body", method = RequestMethod.GET)
    @ResponseBody
    public ResultModel findBody(@CurrentAccount User loginAccount) {
        return ResultModel.OK(service.findBody());
    }

    @RequestMapping(value = "find/code", method = RequestMethod.GET)
    @ResponseBody
    public ResultModel findCode(@CurrentAccount User loginAccount, @RequestParam String code) {
        return ResultModel.OK(service.getContractCode(code));
    }

    @RequestMapping(value = "find/pricelevel", method = RequestMethod.GET)
    @ResponseBody
    public ResultModel findPricelevel(@CurrentAccount User loginAccount) {
        return ResultModel.OK(service.findSetmeal());
    }

    @RequestMapping(value = "find/rebat", method = RequestMethod.GET)
    @ResponseBody
    public ResultModel rebat(@CurrentAccount User loginAccount, @RequestParam String product,
                             @RequestParam Integer level, @RequestParam Long money) {
        return ResultModel.OK(service.getRebat(product, level, money));
    }

    @RequestMapping(value = "find/sale", method = RequestMethod.GET)
    @ResponseBody
    public ResultModel rebat(@CurrentAccount User loginAccount) {
        return ResultModel.OK(service.getSales());
    }


    @RequestMapping(value = "create", method = RequestMethod.POST)
    @ResponseBody
    public ResultModel create(@CurrentAccount User loginAccount, @RequestBody Contract contract, HttpServletRequest request) {
        Contract contract1 = service.create(loginAccount, contract);
        NewUserLogThread userlog = new NewUserLogThread(loginAccount.getEmail(), loginAccount.getName(), OperateObjectTypeEnum.CUSTOMER.getKey(), contract.getContractCode(), "新建合同","",contract1.toString(), request);
        userlog.start();
        return ResultModel.OK(contract1);
    }

    @RequestMapping(value = "update", method = RequestMethod.PUT)
    @ResponseBody
    public ResultModel update(@CurrentAccount User loginAccount, @RequestBody Contract contract) {
        return ResultModel.OK(service.update(loginAccount, contract));
    }

    @RequestMapping(value = "pay", method = RequestMethod.POST)
    @ResponseBody
    public ResultModel pay(@CurrentAccount User loginAccount, @RequestBody ContractMoney contract) {
        return ResultModel.OK(service.pay(loginAccount, contract));
    }

    @RequestMapping(value = "change", method = RequestMethod.POST)
    @ResponseBody
    public ResultModel change(@CurrentAccount User loginAccount, @RequestBody ContractChange contract) {
        return ResultModel.OK(service.change(loginAccount, contract));
    }

    @RequestMapping(value = "find/pay", method = RequestMethod.GET)
    @ResponseBody
    public ResultModel findPay(@CurrentAccount User loginAccount, @RequestParam String startDate,
                               @RequestParam String endDate, @RequestParam String code) {
        return ResultModel.OK(service.findPay(startDate, endDate, code));
    }

    @RequestMapping(value = "find/change", method = RequestMethod.GET)
    @ResponseBody
    public ResultModel findChange(@CurrentAccount User loginAccount, @RequestParam String startDate,
                                  @RequestParam String endDate, @RequestParam String code) {
        return ResultModel.OK(service.findChange(startDate, endDate, code));
    }

    @RequestMapping(value = "update/pay", method = RequestMethod.PUT)
    @ResponseBody
    public ResultModel updatePay(@CurrentAccount User loginAccount, @RequestBody ContractMoney contract) {
        return ResultModel.OK(service.updatePay(loginAccount, contract));
    }

    @RequestMapping(value = "find/payall", method = RequestMethod.GET)
    @ResponseBody
    public ResultModel findPayAll(@CurrentAccount User loginAccount, @RequestParam String startDate,
                               @RequestParam String endDate) {
        return ResultModel.OK(service.findPay(startDate, endDate));
    }


}
