package com.yijia.integral.controller;

import java.util.List;

import com.yijia.common.core.domain.entity.SysDept;
import com.yijia.common.core.domain.model.LoginUser;
import com.yijia.common.utils.SecurityUtils;
import com.yijia.system.service.ISysDeptService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yijia.common.annotation.Log;
import com.yijia.common.core.controller.BaseController;
import com.yijia.common.core.domain.AjaxResult;
import com.yijia.common.enums.BusinessType;
import com.yijia.integral.domain.CustomerPoints;
import com.yijia.integral.service.ICustomerPointsService;
import com.yijia.common.utils.poi.ExcelUtil;
import com.yijia.common.core.page.TableDataInfo;

/**
 * 用户积分Controller
 *
 * @author yijia
 * @date 2021-02-24
 */
@RestController
@RequestMapping("/integral/points")
public class CustomerPointsController extends BaseController
{
    @Autowired
    private ICustomerPointsService customerPointsService;
    @Autowired
    private ISysDeptService deptService;
    /**
     * 查询用户积分列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CustomerPoints customerPoints)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysDept dept =new SysDept();
        dept.setDeptId(currentUser.getUser().getDeptId());
        List<String> list = deptService.selectDeptId(dept);
        if(list!=null && list.size()>0){
            customerPoints.setStationIdList(list);
            customerPoints.setStationId(null);
        }
        startPage();
        List<CustomerPoints> customerPointsList = customerPointsService.selectCustomerPointsList(customerPoints);
        return getDataTable(customerPointsList);
    }

    /**
     * 导出用户积分列表
     */
    @Log(title = "用户积分", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CustomerPoints customerPoints)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysDept dept =new SysDept();
        dept.setDeptId(currentUser.getUser().getDeptId());
        List<String> list = deptService.selectDeptId(dept);
        if(list!=null && list.size()>0){
            customerPoints.setStationIdList(list);
            customerPoints.setStationId(null);
        }
        List<CustomerPoints> customerPointsList = customerPointsService.selectCustomerPointsList(customerPoints);
        ExcelUtil<CustomerPoints> util = new ExcelUtil<CustomerPoints>(CustomerPoints.class);
        return util.exportExcel(customerPointsList, "points");
    }

    /**
     * 获取用户积分详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(customerPointsService.selectCustomerPointsById(id));
    }

    /**
     * 新增用户积分
     */
    @Log(title = "用户积分", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomerPoints customerPoints)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        customerPoints.setStationId(currentUser.getUser().getDeptId());
        customerPoints.setStationName(currentUser.getUser().getDept().getDeptName());
        return toAjax(customerPointsService.insertCustomerPoints(customerPoints));
    }

    /**
     * 修改用户积分
     */
    @Log(title = "用户积分", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerPoints customerPoints)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        customerPoints.setStationId(currentUser.getUser().getDeptId());
        customerPoints.setStationName(currentUser.getUser().getDept().getDeptName());
        return toAjax(customerPointsService.updateCustomerPoints(customerPoints));
    }

    /**
     * 删除用户积分
     */
    @Log(title = "用户积分", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(customerPointsService.deleteCustomerPointsByIds(ids));
    }
}
