package com.ghy.web.controller.customer;

import com.ghy.common.core.controller.BaseController;
import com.ghy.common.core.domain.AjaxResult;
import com.ghy.common.core.page.TableDataInfo;
import com.ghy.common.utils.ExceptionUtil;
import com.ghy.customer.domain.Customer;
import com.ghy.customer.service.CustomerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author clunt
 * 消费者Controller
 */
@Controller
@RequestMapping("/customer")
public class CustomerController extends BaseController {

    private String prefix = "customer";
    
    @Autowired
    private CustomerService customerService;

    @RequiresPermissions("customer:customer:view")
    @GetMapping()
    public String customer()
    {
        return prefix + "/customer";
    }

    @RequiresPermissions("customer:customer:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Customer customer)
    {
        startPage();
        List<Customer> list = customerService.getCustomerList(customer);
        return getDataTable(list);
    }

    @RequiresPermissions("customer:customer:resetPwd")
    @GetMapping("/resetPwd/{customerId}")
    public String resetPwd(@PathVariable("customerId") Long customerId, ModelMap mmap)
    {
        mmap.put("customer", customerService.selectByCustomerId(customerId));
        return prefix + "/resetPwd";
    }

    @PostMapping("/update")
    @ResponseBody
    public AjaxResult updateCustomer(@RequestBody Customer customer){
        try {
            customerService.updateCustomer(customer);
            return AjaxResult.success("更新消费者手机号成功");
        }catch (Exception e){
            ExceptionUtil.getExceptionMessage(e);
            return AjaxResult.error(e.getMessage());
        }
    }


}
