package com.yijia.market.controller;

import java.util.Arrays;
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 com.yijia.system.service.ISysUserService;
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.market.domain.MarkertPlan;
import com.yijia.market.service.IMarkertPlanService;
import com.yijia.common.utils.poi.ExcelUtil;
import com.yijia.common.core.page.TableDataInfo;

/**
 * 营销方案Controller
 *
 * @author yijia
 * @date 2020-12-22
 */
@RestController
@RequestMapping("/market/plan")
public class MarkertPlanController extends BaseController
{
    @Autowired
    private IMarkertPlanService markertPlanService;
    @Autowired
    private ISysDeptService deptService;
    /**
     * 查询营销方案列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MarkertPlan markertPlan)
    {
        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){
            markertPlan.setStationIdList(list);
            markertPlan.setStationId(null);
        }
        markertPlan.setStatus("0");
        startPage();
        List<MarkertPlan> planList = markertPlanService.selectMarkertPlanList(markertPlan);
        return getDataTable(planList);
    }

    /**
     * 导出营销方案列表
     */
    @Log(title = "营销方案", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MarkertPlan markertPlan)
    {
        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){
            markertPlan.setStationIdList(list);
            markertPlan.setStationId(null);
        }
        markertPlan.setStatus("0");
        List<MarkertPlan> planList = markertPlanService.selectMarkertPlanList(markertPlan);
        ExcelUtil<MarkertPlan> util = new ExcelUtil<MarkertPlan>(MarkertPlan.class);
        return util.exportExcel(planList, "plan");
    }

    /**
     * 获取营销方案详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(markertPlanService.selectMarkertPlanById(id));
    }

    /**
     * 新增营销方案
     */
    @Log(title = "营销方案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MarkertPlan markertPlan)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        markertPlan.setStationId(currentUser.getUser().getDeptId());
        markertPlan.setCreateBy(currentUser.getUser().getUserId()+"");
        markertPlan.setCreateTime(DateUtils.getNowDate());
        markertPlan.setStatus("0");
        return toAjax(markertPlanService.insertMarkertPlan(markertPlan));
    }

    /**
     * 修改营销方案
     */
    @Log(title = "营销方案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MarkertPlan markertPlan)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        markertPlan.setStationId(currentUser.getUser().getDeptId());
        markertPlan.setUpdateBy(currentUser.getUser().getUserId()+"");
        markertPlan.setUpdateTime(DateUtils.getNowDate());
        markertPlan.setStatus("0");
        return toAjax(markertPlanService.updateMarkertPlan(markertPlan));
    }

    /**
     * 删除营销方案
     */
    @Log(title = "营销方案", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long ids)
    {
        int i =0;
        MarkertPlan markertPlan =new MarkertPlan();
        LoginUser currentUser = SecurityUtils.getLoginUser();
        markertPlan.setUpdateBy(currentUser.getUser().getUserId()+"");
        markertPlan.setUpdateTime(DateUtils.getNowDate());
        markertPlan.setStatus("1");
        markertPlan.setId(ids);
        i=markertPlanService.updateMarkertPlan(markertPlan);
        return toAjax(i);
    }
}
