package org.linlinjava.jz.admin.web;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.jz.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.jz.admin.util.SecurityHelper;
import org.linlinjava.jz.admin.vo.Ticket_Consult_Type_Vo;
import org.linlinjava.jz.core.util.ResponseUtil;
import org.linlinjava.jz.db.domain.JzDmTicketConsultType;
import org.linlinjava.jz.db.service.JzDmTicketConsultTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

@Api(value = "Admin-Dm-Ticket-Consult-Type-Controller",description = "工单咨询类型维护")
@RestController
@RequestMapping("/admin/ticketConsult")
@Validated
public class AdminDmTicketConsultTypeController {

    private final Log logger = LogFactory.getLog(AdminDmTicketConsultTypeController.class);

    @Autowired
    private JzDmTicketConsultTypeService consultTypeService;


    @Cacheable(cacheNames = "o2oCache", key = "'ticketConsult'")
    @ApiOperation(value = "工单咨询类型查询")
    @RequiresPermissions("admin:ticketConsult:list")
    @RequiresPermissionsDesc(menu = {"工单管理", "咨询类型"}, button = "查询")
    @GetMapping("/list")
    public Object list() {
        List<JzDmTicketConsultType> types = consultTypeService.queryByLevel((short)1);

        //返回值
        List<Ticket_Consult_Type_Vo> consult_type_vos = Lists.newArrayList();

        for (JzDmTicketConsultType type : types) {
            Ticket_Consult_Type_Vo vo = new Ticket_Consult_Type_Vo();
            vo.setId(type.getId());
            vo.setName(type.getName());
            vo.setRemark(type.getRemark());
            vo.setLevel(1);
            vo.setDeleted(type.getDeleted());
            vo.setType(type.getType());
            vo.setPid(type.getPid());
            vo.setHmNoticeType(type.getHmNoticeType());
            consult_type_vos.add(vo);

            //第二级
            List<Ticket_Consult_Type_Vo> childre2 = Lists.newArrayList();
            vo.setChildren(childre2);

            List<JzDmTicketConsultType> consults2 = consultTypeService.queryByPid(type.getId());
            for (JzDmTicketConsultType consult_sub : consults2) {
                Ticket_Consult_Type_Vo vo1 = new Ticket_Consult_Type_Vo();
                vo1.setId(consult_sub.getId());
                vo1.setName(consult_sub.getName());
                vo1.setRemark(consult_sub.getRemark());
                vo1.setLevel(consult_sub.getLevel());
                vo1.setDeleted(consult_sub.getDeleted());
                vo1.setType(consult_sub.getType());
                vo1.setPid(consult_sub.getPid());
                vo1.setHmNoticeType(consult_sub.getHmNoticeType());
                childre2.add(vo1);

                //第三级
                List<Ticket_Consult_Type_Vo> childre3 = Lists.newArrayList();
                List<JzDmTicketConsultType> consults3 = consultTypeService.queryByPid(consult_sub.getId());
                for (JzDmTicketConsultType consult3_sub : consults3) {
                    Ticket_Consult_Type_Vo vo2 = new Ticket_Consult_Type_Vo();
                    vo2.setId(consult3_sub.getId());
                    vo2.setName(consult3_sub.getName());
                    vo2.setRemark(consult3_sub.getRemark());
                    vo2.setLevel(consult3_sub.getLevel());
                    vo2.setDeleted(consult3_sub.getDeleted());
                    vo2.setType(consult3_sub.getType());
                    vo2.setPid(consult3_sub.getPid());
                    vo2.setHmNoticeType(consult3_sub.getHmNoticeType());
                    childre3.add(vo2);
                }
                vo1.setChildren(childre3);
            }

        }

        return ResponseUtil.okList(consult_type_vos);
    }


    @CacheEvict(cacheNames = "o2oCache", key = "'ticketConsult'")
    @ApiOperation(value = "咨询类型新增",response = ResponseUtil.class)
    @RequiresPermissions("admin:ticketConsult:create")
    @RequiresPermissionsDesc(menu = {"工单管理", "咨询类型"}, button = "添加")
    @PostMapping("/create")
    public  Object create(@RequestBody JzDmTicketConsultType jzWoConsultType) {
        jzWoConsultType.setCreateUser(SecurityHelper.getAdminId());
        jzWoConsultType.setUpdateUser(SecurityHelper.getAdminId());
        jzWoConsultType.setDeleted(false);
        jzWoConsultType.setPath(consultTypeService.path(jzWoConsultType));
        consultTypeService.add(jzWoConsultType);
        return ResponseUtil.ok();
    }


    @CacheEvict(cacheNames = "o2oCache", key = "'ticketConsult'")
    @RequiresPermissions("admin:ticketConsult:update")
    @RequiresPermissionsDesc(menu = {"工单管理", "咨询类型"}, button = "编辑")
    @PostMapping("/update")
    public  Object update(@RequestBody JzDmTicketConsultType jzWoConsultType) {
        jzWoConsultType.setUpdateUser(SecurityHelper.getAdminId());
        jzWoConsultType.setPath(consultTypeService.path(jzWoConsultType));
        consultTypeService.update(jzWoConsultType);
        return ResponseUtil.ok();
    }

    @ApiOperation(value = "工单咨询类型读取",response = JzDmTicketConsultType.class)
    @RequiresPermissions("admin:ticketConsult:read")
    @RequiresPermissionsDesc(menu = {"工单管理", "咨询类型"}, button = "详情")
    @PostMapping("/read")
    public  Object read(@NotNull Integer id) {
        return consultTypeService.queryById(id);
    }


    @CacheEvict(cacheNames = "o2oCache", key = "'ticketConsult'")
    @RequiresPermissions("admin:ticketConsult:delete")
    @RequiresPermissionsDesc(menu = {"工单管理", "咨询类型"}, button = "删除")
    @PostMapping("/delete")
    public  Object delete(@RequestBody JzDmTicketConsultType consultType) {
        Integer id = consultType.getId();
        if (id == null) {
            return ResponseUtil.badArgument();
        }
        consultTypeService.delete(id);
        return ResponseUtil.ok();
    }

    @CacheEvict(cacheNames = "o2oCache", key = "'ticketConsult'")
    @RequiresPermissions("admin:ticketConsult:enable")
    @RequiresPermissionsDesc(menu = {"工单管理", "咨询类型"}, button = "启用")
    @PostMapping("/enable")
    public  Object enable(@RequestBody JzDmTicketConsultType consultType) {
        consultType.setUpdateUser(SecurityHelper.getAdminId());
        consultTypeService.enable(consultType);
        return ResponseUtil.ok();
    }




}
