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.IntegralOrder;
import com.yijia.integral.service.IIntegralOrderService;
import com.yijia.common.utils.poi.ExcelUtil;
import com.yijia.common.core.page.TableDataInfo;

/**
 * 积分消费订单Controller
 *
 * @author yijia
 * @date 2021-02-24
 */
@RestController
@RequestMapping("/integral/order")
public class IntegralOrderController extends BaseController
{
    @Autowired
    private IIntegralOrderService integralOrderService;
    @Autowired
    private ISysDeptService sysDeptService;
    /**
     * 查询积分消费订单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(IntegralOrder integralOrder)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysDept dept =new SysDept();
        dept.setDeptId(currentUser.getUser().getDeptId());
        List<String> list = sysDeptService.selectDeptId(dept);
        if(list!=null && list.size()>0){
            integralOrder.setStationIdList(list);
            integralOrder.setStationId(null);
        }
        startPage();
        List<IntegralOrder> orderList = integralOrderService.selectIntegralOrderList(integralOrder);
        return getDataTable(orderList);
    }

    /**
     * 导出积分消费订单列表
     */
    @Log(title = "积分消费订单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(IntegralOrder integralOrder)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysDept dept =new SysDept();
        dept.setDeptId(currentUser.getUser().getDeptId());
        List<String> list = sysDeptService.selectDeptId(dept);
        if(list!=null && list.size()>0){
            integralOrder.setStationIdList(list);
            integralOrder.setStationId(null);
        }
        List<IntegralOrder> orderList = integralOrderService.selectIntegralOrderList(integralOrder);
        ExcelUtil<IntegralOrder> util = new ExcelUtil<IntegralOrder>(IntegralOrder.class);
        return util.exportExcel(orderList, "order");
    }

    /**
     * 获取积分消费订单详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(integralOrderService.selectIntegralOrderById(id));
    }

    /**
     * 新增积分消费订单
     */
    @Log(title = "积分消费订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IntegralOrder integralOrder)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        integralOrder.setStationId(currentUser.getUser().getDeptId());
        integralOrder.setStationName(currentUser.getUser().getDept().getDeptName());
        return toAjax(integralOrderService.insertIntegralOrder(integralOrder));
    }

    /**
     * 修改积分消费订单
     */
    @Log(title = "积分消费订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IntegralOrder integralOrder)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        integralOrder.setStationId(currentUser.getUser().getDeptId());
        integralOrder.setStationName(currentUser.getUser().getDept().getDeptName());
        return toAjax(integralOrderService.updateIntegralOrder(integralOrder));
    }

    /**
     * 删除积分消费订单
     */
    @Log(title = "积分消费订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(integralOrderService.deleteIntegralOrderByIds(ids));
    }
}
