package com.yijia.label.controller;

import java.util.List;

import com.yijia.common.core.domain.model.LoginUser;
import com.yijia.common.utils.SecurityUtils;
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.label.domain.LabelRuleDetail;
import com.yijia.label.service.ILabelRuleDetailService;
import com.yijia.common.utils.poi.ExcelUtil;
import com.yijia.common.core.page.TableDataInfo;

/**
 * 标签规则明细Controller
 * 
 * @author yijia
 * @date 2021-06-15
 */
@RestController
@RequestMapping("/label/detail")
public class LabelRuleDetailController extends BaseController
{
    @Autowired
    private ILabelRuleDetailService labelRuleDetailService;

    /**
     * 查询标签规则明细列表
     */
    @GetMapping("/list")
    public TableDataInfo list(LabelRuleDetail labelRuleDetail)
    {
        startPage();
        List<LabelRuleDetail> list = labelRuleDetailService.selectLabelRuleDetailList(labelRuleDetail);
        return getDataTable(list);
    }

    @GetMapping("/detailList")
    public TableDataInfo detailList(LabelRuleDetail labelRuleDetail)
    {
        labelRuleDetail.setDelFlag("0");
        List<LabelRuleDetail> list = labelRuleDetailService.selectLabelRuleDetailList(labelRuleDetail);
        return getDataTable(list);
    }

    /**
     * 导出标签规则明细列表
     */
    @Log(title = "标签规则明细", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(LabelRuleDetail labelRuleDetail)
    {
        List<LabelRuleDetail> list = labelRuleDetailService.selectLabelRuleDetailList(labelRuleDetail);
        ExcelUtil<LabelRuleDetail> util = new ExcelUtil<LabelRuleDetail>(LabelRuleDetail.class);
        return util.exportExcel(list, "detail");
    }

    /**
     * 获取标签规则明细详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(labelRuleDetailService.selectLabelRuleDetailById(id));
    }

    /**
     * 新增标签规则明细
     */
    @Log(title = "标签规则明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LabelRuleDetail labelRuleDetail)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        labelRuleDetail.setCreateBy(currentUser.getUser().getUserId()+"");
        labelRuleDetail.setDelFlag("0");
        return toAjax(labelRuleDetailService.insertLabelRuleDetail(labelRuleDetail));
    }

    /**
     * 修改标签规则明细
     */
    @Log(title = "标签规则明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LabelRuleDetail labelRuleDetail)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        labelRuleDetail.setUpdateBy(currentUser.getUser().getUserId()+"");
        labelRuleDetail.setDelFlag("1");
        return toAjax(labelRuleDetailService.updateLabelRuleDetail(labelRuleDetail));
    }

    @Log(title = "积分商品管理", businessType = BusinessType.UPDATE)
    @PostMapping(value ="/deleteLabelRuleDetail")
    @Transactional
    public AjaxResult deleteLabelRuleDetail(@RequestBody LabelRuleDetail labelRuleDetail)
    {
        int i=0;
        try {
            LoginUser currentUser = SecurityUtils.getLoginUser();
            labelRuleDetail.setDelFlag("1");
            labelRuleDetail.setUpdateBy(currentUser.getUser().getUserId()+"");
           i= labelRuleDetailService.updateLabelRuleDetail(labelRuleDetail);
        }catch (Exception e){
            System.out.println("------------------------------------"+e.getLocalizedMessage()+"");
            e.printStackTrace();
        }
        return toAjax(i);

    }
    /**
     * 删除标签规则明细
     */
    @Log(title = "标签规则明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(labelRuleDetailService.deleteLabelRuleDetailByIds(ids));
    }

}
