package com.yihu.wlyy.web.doctor.template;

import com.yihu.wlyy.aop.ObserverRequired;
import com.yihu.wlyy.entity.template.DoctorGuidanceTemp;
import com.yihu.wlyy.repository.template.DoctorGuidanceTempDao;
import com.yihu.wlyy.service.template.DoctorGuidanceTempService;
import com.yihu.wlyy.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 医生健康指导模板
 * <p>
 * Created by lyr on 2017/2/3.
 */
@RestController
@RequestMapping(value = "/doctor/guidance_temp")
@Api(description = "医生健康指导模板")
public class DoctorGuidanceTempController extends BaseController {

    @Autowired
    DoctorGuidanceTempService guidanceTempService;
    @Autowired
    DoctorGuidanceTempDao guidanceTempDao;

    /**
     * 查询单个模板详情
     *
     * @return
     */
    @RequestMapping(value = "/listDetail", method = RequestMethod.GET)
    @ApiOperation(value = "查询单个模板详情")
    public String listDetail(@RequestParam @ApiParam(value = "指导模板编码") String modelCode) {
        JSONObject temp = null;
        try {
            temp = guidanceTempService.listDetail(modelCode);
        } catch (Exception e) {
            e.printStackTrace();
            return invalidUserException(e, -1, "查询失败！");
        }
        return write(200, "查询成功！", "data", temp);
    }

    /**
     * 添加指导模板
     *
     * @param content
     * @return
     */
    /*@RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加指导模板")
    public String add(@RequestParam @ApiParam(value = "指导内容") String content) {
        try {
            if (StringUtils.isEmpty(content)) {
                return error(-1, "内容不能为空");
            }

            DoctorGuidanceTemp temp = guidanceTempService.add(getUID(), content);

            if (temp != null) {
                return write(200, "添加成功");
            } else {
                return write(-1, "添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, "添加失败");
        }
    }*/
//  =====================
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加指导模板")
    @ObserverRequired
    public String add(@RequestParam @ApiParam(value = "指导内容") String content,
                      @RequestParam @ApiParam(value = "模板名称") String modelName,
                      @RequestParam(required = false) @ApiParam(value = "图片内容") String imagesUrl) {
        try {
            if (StringUtils.isEmpty(content)) {
                return error(-1, "内容不能为空");
            }
            if (modelName.length() < 1 || modelName.length() > 10) {
                return error(-1, "模板名称不能为空且在10字之内");
            }
            //        验证模板名称唯一性
            List<DoctorGuidanceTemp> templates = guidanceTempDao.findByTitle(getUID(), modelName);
            if (templates != null && templates.size() != 0) {
                return error(-1, "模板名称与现有模板重复，请修改");
            }

            DoctorGuidanceTemp temp = guidanceTempService.add(getUID(), content, modelName, imagesUrl);

            if (temp != null) {
                return write(200, "添加成功");
            } else {
                return write(-1, "添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, "添加失败");
        }
    }
//  =====================

    /**
     * 修改指导模板
     *
     * @param code
     * @param content
     * @return
     */
    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    @ApiOperation(value = "修改指导模板")
    @ObserverRequired
    public String modify(@RequestParam @ApiParam(value = "指导编码") String code,
                         @RequestParam @ApiParam(value = "模板标题") String modelName,
                         @RequestParam @ApiParam(value = "指导内容") String content,
                         @RequestParam(required = false) @ApiParam(value = "图片地址") String imagesUrl
    ) {
        try {
            if (StringUtils.isEmpty(code)) {
                return error(-1, "请指定需修改的模板");
            }

            if (StringUtils.isEmpty(content)) {
                return error(-1, "内容不能为空");
            }

            if (StringUtils.isEmpty(modelName) || modelName.length() > 10) {
                return error(-1, "内容10个汉字之内且不能为空");
            }
//            验证模板名称唯一性 还要去除掉其本身  (判断查出来的和要修改的同名)
            List<DoctorGuidanceTemp> templates = guidanceTempDao.findByTitle(getUID(), modelName);

            for (DoctorGuidanceTemp teamplate : templates) {
                if (!code.equals(teamplate.getCode())) {
//                        不是修改同一个模板
                    return error(-1, "模板名称与现有模板重复，请修改");
                }
            }

            DoctorGuidanceTemp temp = guidanceTempService.modify(code, modelName, content, imagesUrl);

            if (temp != null) {
                return write(200, "修改成功");
            } else {
                return write(-1, "修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, "修改失败");
        }
    }

    /**
     * 删除指导模板
     *
     * @param code
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation(value = "删除指导模板")
    @ObserverRequired
    public String delete(@RequestParam @ApiParam(value = "指导编码") String code) {
        try {
            if (StringUtils.isEmpty(code)) {
                return error(-1, "请指定需删除的模板");
            }

            int result = guidanceTempService.delete(code);

            if (result == 1) {
                return write(200, "删除成功");
            } else {
                return write(-1, "删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, "删除失败");
        }
    }

    /**
     * 查询指导模板
     *
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value = "查询指导模板")
    public String list(@RequestParam(required = false, defaultValue = "")
                       @ApiParam(value = "模板类型 1:系统 2:自定义 为空:所有") String type,
                       @RequestParam(defaultValue = "10") String pageSize,
                       @RequestParam(defaultValue = "1") @ApiParam(value = "当前页码") String pageNo) {
        try {
            int pagesize = Integer.parseInt(pageSize);
            int pageno = Integer.parseInt(pageNo) - 1;
            List<Map<String, Object>> temps = guidanceTempService.list(getUID(), type, pagesize, pageno);

            if (temps == null || temps.size() < 1) {
                return write(200, "查询成功", "data", new JSONArray());
            } else {
                return write(200, "查询成功", "data", new JSONArray(temps));
            }
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, "查询失败");
        }
    }
}
