package userio.controller;


import common.model.*;
import dic.OperateObjectTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import security.annotation.CurrentAccount;
import userio.service.IOAccountService;
import util.ResultModel;
import util.ResultStatus;
import util.UserLogThread;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by mxq on 2017/12/21.
 */
@Controller
@RequestMapping("io/marketing/accountmng")
public class IoAccountController {

    @Autowired
    IOAccountService tkioAccountService;
    //sale
    @RequestMapping(value = "/findSale", method = RequestMethod.GET)
    @ResponseBody
    public ResultModel findSale(@CurrentAccount User loginAccount, @RequestParam(required = false) Long sale) {

        return ResultModel.OK(tkioAccountService.findSale(loginAccount, sale));
    }

    //sale
    @RequestMapping(value = "/update/sale", method = RequestMethod.PUT)
    @ResponseBody
    public ResultModel updateSale(@CurrentAccount User loginAccount,
                                  @RequestParam String email, @RequestParam String accountEmail, HttpServletRequest request) {
        UserLogThread userlog = new UserLogThread(loginAccount.getEmail(), loginAccount.getName(), OperateObjectTypeEnum.CUSTOMER.getKey(), accountEmail, "tkio修改销售为 " + email, request,loginAccount.getRole());
        userlog.start();
        return ResultModel.OK(tkioAccountService.updateSale(email, accountEmail));
    }


    //客户列表
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    @ResponseBody
    public ResultModel find(@CurrentAccount User loginAccount) {
        return ResultModel.OK(tkioAccountService.findAll(loginAccount));
    }

    @RequestMapping(value = "/findone", method = RequestMethod.GET)
    @ResponseBody
    public ResultModel findOne(@CurrentAccount User loginAccount, @RequestParam String email) {
        IOAccount4Web account4Web = tkioAccountService.findOne(email);
        if(null == account4Web){
            return ResultModel.ERROR(ResultStatus.USER_EMPTY);
        }
        return ResultModel.OK(account4Web);
    }
    @RequestMapping(value = "/find/reminderlevel", method = RequestMethod.GET)
    @ResponseBody
    public ResultModel findReminderlevel() {
        return ResultModel.OK(tkioAccountService.findReminderlevel());
    }

    //关闭到期提醒
    @RequestMapping(value = "/forbiden", method = RequestMethod.PUT)
    @ResponseBody
    public ResultModel forbiden(@CurrentAccount User loginAccount, @RequestBody Reminder resource, HttpServletRequest request) {
        UserLogThread userlog = new UserLogThread(loginAccount.getEmail(), loginAccount.getName(), OperateObjectTypeEnum.CUSTOMER.getKey(), resource.getEmail(), "关闭异常状态跟进", request,loginAccount.getRole());
        userlog.start();
        return ResultModel.OK(tkioAccountService.forbiden(loginAccount, resource));
    }

    //开启到期提醒
    @RequestMapping(value = "/enable", method = RequestMethod.PUT)
    @ResponseBody
    public ResultModel enable(@CurrentAccount User loginAccount, @RequestBody Reminder resource, HttpServletRequest request) {
        UserLogThread userlog = new UserLogThread(loginAccount.getEmail(), loginAccount.getName(), OperateObjectTypeEnum.CUSTOMER.getKey(), resource.getEmail(), "开启异常状态跟进", request,loginAccount.getRole());
        userlog.start();
        return ResultModel.OK(tkioAccountService.enable(loginAccount, resource));
    }

    //关闭缴费提醒
    @RequestMapping(value = "/forbidenpress", method = RequestMethod.PUT)
    @ResponseBody
    public ResultModel forbidenPress(@CurrentAccount User loginAccount, @RequestBody PressMoney resource, HttpServletRequest request) {
        UserLogThread userlog = new UserLogThread(loginAccount.getEmail(), loginAccount.getName(), OperateObjectTypeEnum.CUSTOMER.getKey(), resource.getEmail(), "关闭异常状态跟进", request,loginAccount.getRole());
        userlog.start();
        return ResultModel.OK(tkioAccountService.forbidenPress(loginAccount, resource));
    }

    //开启缴费提醒
    @RequestMapping(value = "/enablepress", method = RequestMethod.PUT)
    @ResponseBody
    public ResultModel enablePress(@CurrentAccount User loginAccount, @RequestBody PressMoney resource, HttpServletRequest request) {
        UserLogThread userlog = new UserLogThread(loginAccount.getEmail(), loginAccount.getName(), OperateObjectTypeEnum.CUSTOMER.getKey(), resource.getEmail(), "开启异常状态跟进", request,loginAccount.getRole());
        userlog.start();
        return ResultModel.OK(tkioAccountService.enablePress(loginAccount, resource));
    }

    //缴费
    @RequestMapping(value = "/pay", method = RequestMethod.PUT)
    @ResponseBody
    public ResultModel pay(@CurrentAccount User loginAccount, @RequestBody Payment resource, HttpServletRequest request) {
        UserLogThread userlog = new UserLogThread(loginAccount.getEmail(), loginAccount.getName(), OperateObjectTypeEnum.CUSTOMER.getKey(), resource.getEmail(), "缴费", request,loginAccount.getRole());
        userlog.start();
        return ResultModel.OK(tkioAccountService.pay(loginAccount, resource));
    }

    //回访
    @RequestMapping(value = "/visit", method = RequestMethod.PUT)
    @ResponseBody
    public ResultModel visit(@CurrentAccount User loginAccount, @RequestBody BackVisit resource, HttpServletRequest request) {
        UserLogThread userlog = new UserLogThread(loginAccount.getEmail(), loginAccount.getName(), OperateObjectTypeEnum.CUSTOMER.getKey(), resource.getEmail(), "回访", request,loginAccount.getRole());
        userlog.start();
        return ResultModel.OK( tkioAccountService.visit(loginAccount, resource));
    }
    //功能使用查询
    @RequestMapping(value = "/find/function/{accountId}", method = RequestMethod.GET)
    @ResponseBody
    public ResultModel updateName(@CurrentAccount User loginAccount, @RequestParam String startDate,
                                  @RequestParam String endDate,  @PathVariable Long accountId) {

        return ResultModel.OK(tkioAccountService.findFunTimes(accountId,startDate,endDate));
    }

    //功能使用查询
    @RequestMapping(value = "/find/pv/{accountId}", method = RequestMethod.GET)
    @ResponseBody
    public ResultModel pv(@CurrentAccount User loginAccount, @RequestParam String startDate,
                                  @RequestParam String endDate,  @PathVariable Long accountId) {

        return ResultModel.OK(tkioAccountService.findPv(accountId,startDate,endDate));
    }

    //到期提醒查询
    @RequestMapping(value = "/find/reminder/{accountId}", method = RequestMethod.GET)
    @ResponseBody
    public ResultModel findRed(@CurrentAccount User loginAccount, @RequestParam String startDate,
                          @RequestParam String endDate,  @PathVariable Long accountId) {

        return ResultModel.OK(tkioAccountService.findRed(accountId,startDate,endDate));
    }

    //回访查询
    @RequestMapping(value = "/find/visit/{accountId}", method = RequestMethod.GET)
    @ResponseBody
    public ResultModel findVisit(@CurrentAccount User loginAccount, @RequestParam String startDate,
                               @RequestParam String endDate,  @PathVariable Long accountId) {

        return ResultModel.OK(tkioAccountService.findVisit(accountId,startDate,endDate));
    }

    //缴费查询
    @RequestMapping(value = "/find/pay/{accountId}", method = RequestMethod.GET)
    @ResponseBody
    public ResultModel findPay(@CurrentAccount User loginAccount, @RequestParam String startDate,
                               @RequestParam String endDate,  @RequestParam String email,  @PathVariable Long accountId) {

        return ResultModel.OK(tkioAccountService.findPay(email,startDate,endDate));
    }

    //套餐变更查询
    @RequestMapping(value = "/find/contract/{accountId}", method = RequestMethod.GET)
    @ResponseBody
    public ResultModel findChange(@CurrentAccount User loginAccount,  @RequestParam String email,  @PathVariable Long accountId) {

        return ResultModel.OK(tkioAccountService.findContract(email));
    }

    //合同查询
    @RequestMapping(value = "/find/change/{accountId}", method = RequestMethod.GET)
    @ResponseBody
    public ResultModel findContract(@CurrentAccount User loginAccount, @RequestParam String startDate,
                                  @RequestParam String endDate,  @RequestParam String email,  @PathVariable Long accountId) {

        return ResultModel.OK(tkioAccountService.findContractChange(email,startDate,endDate));
    }
}
