package com.yijia.label.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.DateUtils;
import com.yijia.common.utils.SecurityUtils;
import com.yijia.common.utils.poi.ExcelUtil;
import com.yijia.label.domain.LabelRule;
import com.yijia.label.domain.LabelRuleDetail;
import com.yijia.label.domain.RequsetSysDept;
import com.yijia.label.service.ILabelRuleDetailService;
import com.yijia.label.service.ILabelRuleService;
import com.yijia.system.service.ISysDeptService;
import org.springframework.beans.BeanUtils;
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.common.core.page.TableDataInfo;

/**
 * 标签规则
Controller
 * 
 * @author yijia
 * @date 2021-06-15
 */
@RestController
@RequestMapping("/label/rule")
public class LabelRuleController extends BaseController
{
    @Autowired
    private ILabelRuleService labelRuleService;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private ILabelRuleDetailService labelRuleDetailService;
    /**
     * 查询标签规则列表
     */
    @GetMapping("/list")
    public TableDataInfo list(LabelRule labelRule)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        labelRule.setStationId(currentUser.getUser().getDeptId());
        labelRule.setDelFlag("0");
        startPage();
        List<LabelRule> list = labelRuleService.selectLabelRuleList(labelRule);
        return getDataTable(list);
    }

    /**
     * 导出标签规则列表
     */
    @Log(title = "标签规则", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(LabelRule labelRule)
    {
        List<LabelRule> list = labelRuleService.selectLabelRuleList(labelRule);
        ExcelUtil<LabelRule> util = new ExcelUtil<LabelRule>(LabelRule.class);
        return util.exportExcel(list, "rule");
    }

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

    /**
     *标签功能是否开启,标签名
     */

    @GetMapping("/sysDeptDemoList")
    public AjaxResult sysDeptDemoList()
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysDept dept = deptService.selectDeptById(currentUser.getUser().getDeptId());
        RequsetSysDept requsetSysDept = new RequsetSysDept();
        BeanUtils.copyProperties(dept,requsetSysDept);
        LabelRule labelRule =new LabelRule();
        labelRule.setStationId(currentUser.getUser().getDeptId());
        labelRule.setDelFlag("0");
        List<LabelRule> list = labelRuleService.selectLabelRuleList(labelRule);
        requsetSysDept.setLabelArr(list);
        return AjaxResult.success(requsetSysDept);
    }

    @GetMapping("/selectLabelList")
    public AjaxResult selectLabelList()
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysDept dept = deptService.selectDeptById(currentUser.getUser().getDeptId());
        RequsetSysDept requsetSysDept = new RequsetSysDept();
        BeanUtils.copyProperties(dept,requsetSysDept);
        if(requsetSysDept!=null && requsetSysDept.getLabelFlag()!=null && requsetSysDept.getLabelFlag().equals("1")){
            LabelRule labelRule =new LabelRule();
            labelRule.setStationId(currentUser.getUser().getDeptId());
            labelRule.setDelFlag("0");
            startPage();
            List<LabelRule> list = labelRuleService.selectLabelRuleList(labelRule);
            if(list!=null && list.size()>0){
                for(LabelRule rule:list){
                    LabelRuleDetail detail =new LabelRuleDetail();
                    detail.setParentId(rule.getId());
                    detail.setDelFlag("0");
                    List<LabelRuleDetail> labelRuleDetailList = labelRuleDetailService.selectLabelRuleDetailList(detail);
                    if(labelRuleDetailList!=null && labelRuleDetailList.size()>0){
                        rule.setLabelRuleDetailList(labelRuleDetailList);
                    }
                }
            }
            requsetSysDept.setLabelArr(list);
        }
        return AjaxResult.success(requsetSysDept);
    }
    @Log(title = "标签规则", businessType = BusinessType.INSERT)
    @PostMapping("/addlabel")
    @Transactional
    public AjaxResult addlabel(@RequestBody RequsetSysDept requsetSysDept)
    {
        int i =0;
        LoginUser currentUser = SecurityUtils.getLoginUser();
        //根据demo获取添加数据
        if(requsetSysDept!=null){
            SysDept dept =new SysDept();
            dept.setDeptId(currentUser.getUser().getDeptId());
            dept.setLabelFlag(requsetSysDept.getLabelFlag());
            dept.setUpdateBy(currentUser.getUser().getUserId()+"");
            dept.setUpdateTime(DateUtils.getNowDate());
            i =  deptService.updateDeptInfo(dept);
            if(requsetSysDept.getLabelArr()!=null && requsetSysDept.getLabelArr().size()>0){
                for(LabelRule labelRule : requsetSysDept.getLabelArr()) {
                    if(labelRule.getId()!=null){
                        labelRule.setUpdateBy(currentUser.getUser().getUserId()+"");
                        i=labelRuleService.updateLabelRule(labelRule);
                    }else{
                        labelRule.setStationId(currentUser.getUser().getDeptId());
                        labelRule.setCreateBy(currentUser.getUser().getUserId()+"");
                        labelRule.setDelFlag("0");
                        i=labelRuleService.insertLabelRule(labelRule);
                    }
                }
            }
            if(requsetSysDept.getDelArr()!=null &&requsetSysDept.getDelArr().size()>0){
                List<Integer> idlist =requsetSysDept.getDelArr();
                Integer[] ints = idlist.toArray(new Integer[0]);
                for(Integer ids :idlist){
                    LabelRule labelRule =new LabelRule();
                    labelRule.setId(ids);
                    labelRule.setDelFlag("1");
                    labelRule.setUpdateBy(currentUser.getUser().getUserId()+"");
                    labelRuleService.updateLabelRule(labelRule);
                    LabelRuleDetail labelRuleDetail =new LabelRuleDetail();
                    labelRuleDetail.setParentId(ids);
                    List<LabelRuleDetail> detailList =labelRuleDetailService.selectLabelRuleDetailList(labelRuleDetail);
                    for( LabelRuleDetail detail:detailList){
                        detail.setDelFlag("1");
                        detail.setUpdateBy(currentUser.getUser().getUserId()+"");
                        labelRuleDetailService.updateLabelRuleDetail(detail);
                    }
                }
            }
        }
        return toAjax(i);
    }

    /**
     * 修改标签规则
     */
    @Log(title = "标签规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LabelRule labelRule)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        labelRule.setUpdateBy(currentUser.getUser().getUserId()+"");
        return toAjax(labelRuleService.updateLabelRule(labelRule));
    }

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