package com.dgtis.dorm.manager.admin.web;

import com.github.pagehelper.PageInfo;
import com.dgtis.dorm.manager.admin.annotation.LoginAdmin;
import com.dgtis.dorm.manager.db.domain.Admin;
import com.dgtis.dorm.manager.db.domain.QuestionsAnswers;
import com.dgtis.dorm.manager.db.domain.ZhllConsultingComplaints;
import com.dgtis.dorm.manager.db.service.AdminService;
import com.dgtis.dorm.manager.db.service.DeptInfoService;
import com.dgtis.dorm.manager.db.service.ZhllConsultingComplaintsService;
import com.dgtis.dorm.manager.db.util.ResponseUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author qxp
 * @version 1.0
 * @descption: TODO
 * @company 神州数码通用软件（洛阳）有限公司
 * @copyright (c) 2019 LuoYang DGT Co'Ltd Inc. All rights reserved.
 * @date 19-8-5 14:57
 * @since JDK1.8
 */
@RestController
@RequestMapping("/admin/consultant")
public class ConsultantController {
    private final Log logger = LogFactory.getLog(ConsultantController.class);

    @Autowired
    private ZhllConsultingComplaintsService zhllConsultingComplaintsService;

    @Autowired
    private AdminService adminService;

    /**
     * @descption: 根据用户信息和条件查询咨询记录列表
     * @param: [adminId, isAnswer, content, deptUnid, page, limit]
     * @return: java.lang.Object
     * @auther: qxp
     * @date:19-8-6 17:48
     */
    @GetMapping("/list")
    public Object list(@LoginAdmin Integer adminId,
                       String isAnswer,
                       String content,
                       String deptUnid,
                       String type,
                       Integer page,
                       Integer limit,
                       String startDate,
                       String endDate){
        if(adminId==null){
            return ResponseUtil.unlogin();
        }
        Map<String, Object> result = new HashMap<String, Object>();
        //查找adminId所属的部门Unid
        Admin admin = adminService.selectByPrimaryKey(adminId);
        Map<String,Object> userInfo = new HashMap<>() ;
        if(admin.getUserId()!=null){
            userInfo=adminService.getUserInfoByUserId(admin.getUserId().toString());
        }
        boolean canGetALl = false;
        if(userInfo.containsKey("postCode")){
            if(userInfo.get("postCode").equals("10001")){//等于中心领导
                result.put("canGetAll",true);
                canGetALl = true;
            }else{
                result.put("canGetAll",false);
            }

        }

        if(deptUnid==null){
            //如果用户不是管理员，又没有传递部门id，本身又不属于任何部门则不查询数据
            if(userInfo.get("deptUnid")!=null){
                deptUnid =userInfo.get("deptUnid").toString();
            }else{
                if(!canGetALl){
                    result.put("items", null);
                    result.put("total", 0);
                    result.put("deptUnid","None");
                    return ResponseUtil.ok(result);
                }
            }
        }

        ZhllConsultingComplaints zhllConsultingComplaints = new ZhllConsultingComplaints();
        if(deptUnid!=null){
            zhllConsultingComplaints.setDeptUnid(deptUnid);
        }
        if(isAnswer!=null){
            zhllConsultingComplaints.setIsAnswer(isAnswer);
        }
        if(content!=null){
            zhllConsultingComplaints.setContent(content);
        }
        if(type!=null){
            zhllConsultingComplaints.setType(type);
        }

        PageInfo<ZhllConsultingComplaints> pageInfo =zhllConsultingComplaintsService.queryByTimeListPage(zhllConsultingComplaints,startDate,endDate,page,limit);

        result.put("items", pageInfo.getList());
        result.put("total", pageInfo.getTotal());
        result.put("deptUnid",deptUnid);

        return ResponseUtil.ok(result);
    }

    /**
     * 获取指定id的咨询详情
     */
    @GetMapping("read")
    public Object read(@LoginAdmin Integer adminId, String id) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }

        if(id == null) {
            return  ResponseUtil.badArgumentValue();
        }

        ZhllConsultingComplaints zhllConsultingComplaints = zhllConsultingComplaintsService.selectZhllConsultingComplaintsById(id);

        return ResponseUtil.ok(zhllConsultingComplaints);
    }

    /**
     * 更新问题
     */
    @PostMapping("answer")
    public Object answer(@LoginAdmin Integer adminId, @RequestBody ZhllConsultingComplaints zhllConsultingComplaints) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }
        zhllConsultingComplaints.setIsAnswer("1");
        zhllConsultingComplaintsService.updateZhllConsultingComplaints(zhllConsultingComplaints);

        return ResponseUtil.ok();
    }

    /**
     * 删除问题
     */
    @GetMapping("delete")
    public Object delete(@LoginAdmin Integer adminId, String id) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }

        if(id == null) {
            return ResponseUtil.badArgumentValue();
        }

        ZhllConsultingComplaints zh =new ZhllConsultingComplaints();
        zh.setId(id);
        zh.setDelFlag("1");
        zhllConsultingComplaintsService.updateZhllConsultingComplaints(zh);
        return ResponseUtil.ok();
    }

}


