package com.yijia.integral.controller;

import java.util.ArrayList;
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.DateUtils;
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.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
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.IntegralWares;
import com.yijia.integral.service.IIntegralWaresService;
import com.yijia.common.utils.poi.ExcelUtil;
import com.yijia.common.core.page.TableDataInfo;

/**
 * 积分商品管理Controller
 *
 * @author yijia
 * @date 2021-03-01
 */
@RestController
@RequestMapping("/integral/wares")
public class IntegralWaresController extends BaseController
{
    @Autowired
    private IIntegralWaresService integralWaresService;
    @Autowired
    private ISysDeptService sysDeptService;
    /**
     * 查询积分商品管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(IntegralWares integralWares)
    {
        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){
            integralWares.setStationIdList(list);
            integralWares.setStationId(null);
        }
        List<String> wareslist =new ArrayList<>();
        wareslist.add("1");
        wareslist.add("2");
        integralWares.setWaresStatusList(wareslist);
        startPage();
        List<IntegralWares> waresList = integralWaresService.selectIntegralWaresList(integralWares);
        return getDataTable(waresList);
    }

    /**
     * 导出积分商品管理列表
     */
    @Log(title = "积分商品管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(IntegralWares integralWares)
    {
        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){
            integralWares.setStationIdList(list);
            integralWares.setStationId(null);
        }
        integralWares.setWaresStatus("1");
        List<IntegralWares> waresList = integralWaresService.selectIntegralWaresList(integralWares);
        ExcelUtil<IntegralWares> util = new ExcelUtil<IntegralWares>(IntegralWares.class);
        return util.exportExcel(waresList, "wares");
    }

    /**
     * 获取积分商品管理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(integralWaresService.selectIntegralWaresById(id));
    }

    /**
     * 新增积分商品管理
     */
    @Log(title = "积分商品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IntegralWares integralWares)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        integralWares.setCreateBy(currentUser.getUser().getUserId().toString());
        integralWares.setCreateTime(DateUtils.getNowDate());
        integralWares.setStationId(currentUser.getUser().getDeptId());
        integralWares.setWaresStatus("1");
        return toAjax(integralWaresService.insertIntegralWares(integralWares));
    }

    /**
     * 修改积分商品管理。默认删除再做新增
     */
    @Log(title = "积分商品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @Transactional
    public AjaxResult edit(@RequestBody IntegralWares integralWares)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        integralWares.setStationId(currentUser.getUser().getDeptId());
        integralWares.setUpdateBy(currentUser.getUser().getUserId().toString());
        integralWares.setUpdateTime(DateUtils.getNowDate());
        return toAjax(integralWaresService.updateIntegralWares(integralWares));
    }
    /**
     * 修改积分商品管理
     */
    @Log(title = "积分商品管理", businessType = BusinessType.UPDATE)
    @PostMapping(value ="/editInfoWares")
    //@ResponseBody
    public AjaxResult editInfoWares(@RequestBody IntegralWares integralWares)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        integralWares.setStationId(currentUser.getUser().getDeptId());
        integralWares.setUpdateBy(currentUser.getUser().getUserId().toString());
        integralWares.setUpdateTime(DateUtils.getNowDate());
        return toAjax(integralWaresService.editInfoWares(integralWares));
    }


    /**
     * 删除积分商品管理
     */
    @Log(title = "积分商品管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(integralWaresService.deleteIntegralWaresByIds(ids));
    }
}
