package com.yijia.markert.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.common.utils.StringUtils;
import com.yijia.common.utils.poi.ExcelUtil;
import com.yijia.markert.domain.MarkertPlan;
import com.yijia.markert.service.IMarkertPlanService;
import com.yijia.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
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.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 =new ArrayList<>();
        if(markertPlan.getDiscountPlanType()!=null && markertPlan.getDiscountPlanType().equals("4")){
            planList  = markertPlanService.searchMarkertPlanList(markertPlan);
        }else{
            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);
    }

    /**
     * 新增阶梯营销方案
     */
    @Log(title = "营销方案", businessType = BusinessType.INSERT)
    @PostMapping("/addMarketPlanInfo")
    @Transactional
    public AjaxResult addMarketPlanInfo(@RequestBody MarkertPlan markertPlan)
    {
        int i =0;
        LoginUser currentUser = SecurityUtils.getLoginUser();
        //查询所有的数据
        MarkertPlan plans =new MarkertPlan();
        plans.setDiscountPlanType("4");
        plans.setStationId(currentUser.getUser().getDeptId());
        plans.setOilName(markertPlan.getOilName());
        List<MarkertPlan> markertPlans  =markertPlanService.selectMarkertPlanList(plans);
        if(markertPlans!=null && markertPlans.size()>0){
            for(MarkertPlan plan:markertPlans){
                i= markertPlanService.deleteMarkertPlanById(plan.getId());
            }
        }
        if(markertPlan!=null && markertPlan.getList()!=null && markertPlan.getList().size()>0){
            for(MarkertPlan plan:markertPlan.getList()){
                plan.setStationId(currentUser.getUser().getDeptId());
                plan.setCreateBy(currentUser.getUser().getUserId()+"");
                plan.setCreateTime(DateUtils.getNowDate());
                plan.setStatus("0");
                plan.setGrade(markertPlan.getGrade());
                plan.setDiscountTerm(markertPlan.getDiscountTerm());
                plan.setVipDiscountyPlus(markertPlan.getVipDiscountyPlus());
                plan.setCouponPlus(markertPlan.getCouponPlus());
                plan.setOilName(markertPlan.getOilName());
                plan.setDiscountPlanType(markertPlan.getDiscountPlanType());
                i= markertPlanService.insertMarkertPlan(plan);
            }
        }
        return toAjax(i);
    }

    /**
     * 删除营销方案
     */
    @Log(title = "删除营销方案", businessType = BusinessType.DELETE)
    @PostMapping("/deleteMarketPlan")
    public AjaxResult deleteMarketPlan(@RequestBody MarkertPlan markertPlan)
    {
       int i=0;
        LoginUser currentUser = SecurityUtils.getLoginUser();
       if(markertPlan!=null && markertPlan.getOilName()!=null) {
         markertPlan.setStationId(currentUser.getUser().getDeptId());
         i = markertPlanService.deleteMarkertPlan(markertPlan);
       }
       return toAjax(i);
    }
}
