package com.sunxung.factoring.web.customerinformation;

import com.sunxung.factoring.component.annotation.OperationLog;
import com.sunxung.factoring.component.enums.OperationTypeEnum;
import com.sunxung.factoring.component.util.ConstantUtil;
import com.sunxung.factoring.entity.GridPage;
import com.sunxung.factoring.entity.ResponseJson;
import com.sunxung.factoring.entity.customerinformation.Customer;
import com.sunxung.factoring.entity.customerinformation.SupplierBankCardInfo;
import com.sunxung.factoring.entity.vo.ConditionVo;
import com.sunxung.factoring.service.customerinformation.CustomerService;
import com.sunxung.factoring.service.customerinformation.SupplierBankCardInfoService;
import com.sunxung.factoring.service.sys.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 客户模块
 *
 * @author liutao
 */
@Controller
@RequestMapping("customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private SupplierBankCardInfoService supplierBankCardInfoService;

    @Autowired
    private UserService userService;

    /**
     * 跳转supplierList页面
     *
     * @return
     */
    @RequestMapping("listPage")
    public String listPage() {
        return ConstantUtil.CUSTOMER_PREFIX + "supplierList";
    }

    /**
     * 跳转enterpriseList页面
     *
     * @return
     */
    @RequestMapping("enterpriseList")
    public String enterpriseList() {
        return ConstantUtil.CUSTOMER_PREFIX + "enterpriseList";
    }

    /**
     * 跳转enterpriseList页面
     *
     * @return
     */
    @RequestMapping("personalList")
    public String personalList() {
        return ConstantUtil.CUSTOMER_PREFIX + "personalList";
    }

    /**
     * 跳转供应商添加页面
     *
     * @return
     */
    @RequestMapping("addPage")
    public String addPage() {
        return ConstantUtil.CUSTOMER_PREFIX + "supplierAdd";
    }

    /**
     * 跳转核心企业添加页面
     *
     * @return
     */
    @RequestMapping("enterpriseAdd")
    public String enterpriseAdd() {
        return ConstantUtil.CUSTOMER_PREFIX + "enterpriseAdd";
    }

    /**
     * 从第三方获取企业客户信息（sop）
     *
     * @return
     */
    @RequestMapping("/doFindEnterpriseBySop")
    @ResponseBody
    public Object doFindEnterpriseBySop(ConditionVo conditionVo) {
        return customerService.findEnterpriseBySop(conditionVo);
    }


    /**
     * 根据企业名称查询企业客户列表
     *
     * @param conditionVo 查询条件
     * @return 符合条件的企业客户列表
     */
    @RequestMapping("findList")
    @ResponseBody
    public GridPage<Customer> findList(ConditionVo conditionVo) {
        return customerService.findList(conditionVo);
    }

    /**
     * 根据企业名称查询企业客户列表
     *
     * @param conditionVo 查询条件
     * @return 符合条件的企业客户列表
     */
    @RequestMapping("findListForC")
    @ResponseBody
    public Object findListForC(ConditionVo conditionVo) {
        return customerService.findList(conditionVo);
    }

    /**
     * 查询所有核心企业或供应商
     *
     * @param supplier
     * @return 所有核心企业或供应商
     */
    @RequestMapping("findCoreBusiness")
    @ResponseBody
    public ResponseJson findCoreBusiness(boolean supplier) {
        return new ResponseJson(customerService.findCoreBusiness(supplier));
    }

    /**
     * 新增客户信息
     *
     * @param customer
     * @return
     */
    @RequestMapping("add")
    @ResponseBody
    @OperationLog(operationModule = "客户模块",operationType = OperationTypeEnum.INSERT,description = "新增客户信息")
    public ResponseJson add(Customer customer) throws Exception {
        customerService.add(customer);
        return new ResponseJson("新增成功");
    }

    /**
     * 重新生成认证链接
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("flushUrl")
    @ResponseBody
    @OperationLog(operationModule = "客户模块",operationType = OperationTypeEnum.OTHER,description = "重新生成认证链接")
    public ResponseJson flushUrl(Long id) throws Exception {
        customerService.flushUrl(id);
        return new ResponseJson("认证链接刷新成功");
    }


    /**
     * 重新生成认证链接和签署链接
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("flushUrlByCompany")
    @ResponseBody
    public ResponseJson flushUrlByCompany(Long id) throws Exception {
        customerService.flushUrlByCompany(id);
        return new ResponseJson("认证链接刷新成功");
    }



    @ResponseBody
    @RequestMapping("flushStatusBySop")
    public ResponseJson flushUrlByTrade(String code,Integer type){
        customerService.flushStatusBySop(code,type);
        return new ResponseJson("刷新成功");
    }

    /**
     * 查询详情
     *
     * @param id
     * @return
     */
    @RequestMapping("findDetail")
    public String findDetail(long id, Model model) {
        Customer detail = customerService.findDetail(id);
        model.addAttribute("detail", detail);
        return ConstantUtil.CUSTOMER_PREFIX + "supplierDetail";
    }

    @PostMapping("/deleteCustomer")
    @ResponseBody
    @OperationLog(operationModule = "客户模块",operationType = OperationTypeEnum.DELETE,description = "删除企业")
    public ResponseJson deleteCustomer(Long id) {
        customerService.deleteCustomer(id);
        return new ResponseJson("删除企业成功！");
    }

    /**
     * 从sop更新企业信息
     *
     * @return
     */
    @RequestMapping("flush")
    @ResponseBody
    @OperationLog(operationModule = "客户模块",operationType = OperationTypeEnum.OTHER,description = "从sop更新企业信息")
    public ResponseJson flush() {
        customerService.flush();
        return new ResponseJson("同步成功");
    }

    /**
     * 修改账户名称
     *
     * @param customer
     * @return
     */
    @RequestMapping("modifyAccount")
    @ResponseBody
    @OperationLog(operationModule = "客户模块",operationType = OperationTypeEnum.UPDATE,description = "修改账户名称")
    public ResponseJson modifyAccount(Customer customer) {
        customerService.modifyAccount(customer);
        return new ResponseJson("修改成功");
    }

    /**
     * 修改账户密码
     *
     * @param customer
     * @return
     */
    @RequestMapping("modifyPassword")
    @ResponseBody
    @OperationLog(operationModule = "客户模块",operationType = OperationTypeEnum.UPDATE,description = "修改账户密码")
    public ResponseJson modifyPassword(Customer customer) {
        customerService.modifyPassword(customer);
        return new ResponseJson(ConstantUtil.PASSWORD);
    }

    /**
     * 查询详情
     *
     * @param id
     * @return
     */
    @RequestMapping("findBankDetail")
    public String findBankDetail(long id, Model model) {
        Customer customer = customerService.findDetail(id);
        List<SupplierBankCardInfo> list = supplierBankCardInfoService.findBySupplierId(id);
        customer.setBanks(list);
        model.addAttribute("bankDetail", customer);
        return ConstantUtil.CUSTOMER_PREFIX + "supplierBankAccount";
    }

    /**
     * 供应商下的银行账户信息添加或删除
     *
     * @param
     * @return
     */
    @RequestMapping("modifyBankDetail")
    @ResponseBody
    @OperationLog(operationModule = "客户模块",operationType = OperationTypeEnum.OTHER,description = "供应商下的银行账户信息添加或删除")
    public ResponseJson modifyBankDetail(Customer customer, long supplierId) {
        customerService.modifyBankDetail(customer, supplierId);
        return new ResponseJson("操作成功");
    }

    /**
     * 获取业务经理创建的所有业务下关联的供应商
     *
     * @param
     * @return
     */
    @RequestMapping("getLocalUserSuppliers")
    @ResponseBody
    public ResponseJson getLocalUserSuppliers() {
        return new ResponseJson(customerService.getLocalUserSuppliers());
    }

    /**
     * 经理创建的业务中关联的某一个供应商的业务中的核心企业名称。
     *
     * @param
     * @return
     */
    @RequestMapping("getLocalUserCustomers")
    @ResponseBody
    public ResponseJson getLocalUserCustomers(Long supplierId) {
        return new ResponseJson(customerService.getLocalUserCustomers(supplierId));
    }

    /**
     * 经理创建的业务中的核心企业名称。
     *
     * @param
     * @return
     */
    @RequestMapping("getCustomersByUser")
    @ResponseBody
    public ResponseJson getCustomersByUser() {
        return new ResponseJson(customerService.getCustomersByUser());
    }

    /**
     * 经理创建的业务中的供应商。
     *
     * @param
     * @return
     */
    @RequestMapping("getSuppliersByUser")
    @ResponseBody
    public ResponseJson getSuppliersByUser(String businessNumber) {
        return new ResponseJson(customerService.getSuppliersByUser(businessNumber));
    }
}
