package com.palmnest.webapp.controller.backstage;

import cn.superdesk.libs.common.json.JsonUtils;
import com.palmnest.application.AppConstants;
import com.palmnest.application.bean.AppResultDto;
import com.palmnest.application.core.util.AppResultUtil;
import com.palmnest.application.core.util.LogExceptionStackUtil;
import com.palmnest.application.core.util.StringUtil;
import com.palmnest.application.dto.WebPageResultDto;
import com.palmnest.application.dto.input.*;
import com.palmnest.dao.repair.RepairMaterialDao;
import com.palmnest.model.User;
import com.palmnest.model.repair.RepairAddress;
import com.palmnest.model.repair.RepairBill;
import com.palmnest.model.repair.RepairMaterial;
import com.palmnest.model.repair.RepairReturnVisit;
import com.palmnest.service.repair.RepairMaterialManager;
import com.palmnest.webapp.controller.BaseFormController;
import org.directwebremoting.util.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 服务之窗1L- RepairMaterialController
 *
 * @Description TODO
 * @author: jiangyong
 * @since: 2020/8/13 11:26
 */
@Controller
@RequestMapping(value = "/repairMaterial")
public class RepairMaterialController extends BaseFormController {
    private Logger LOG = Logger.getLogger(RepairTypeController.class);
    @Autowired
    private RepairMaterialManager repairMaterialManager;

    @RequestMapping(value = "/addOrUpdate")
    @ResponseBody
    public WebPageResultDto addOrUpdate(
            @RequestParam(value = "id", required = false) Long id, @RequestParam(value = "orgId") Integer orgId,
            @RequestParam(value = "materialName") String materialName,
            @RequestParam(value = "description", required = false) String description, HttpServletRequest request) {

        LOG.info("RepairMaterialController.addOrUpdate \n id:{" + id + "},orgId:{" + orgId + "},materialName:{" + materialName + "},description:{" + description + "}");

        try {
            User user = (User) request.getSession().getAttribute("UserCache");
            if (id == null) { //新增
                RepairMaterial repairMaterial = new RepairMaterial();
                repairMaterial.setEnabled(1);
                repairMaterial.setOrgId(orgId);
                repairMaterial.setMaterialName(materialName);
                if (StringUtil.isNotEmpty(description)) {
                    repairMaterial.setDescription(description);
                }
                repairMaterial.setCreateTime(new Date());
                repairMaterial.setCreatorId(user.getId().intValue());
                repairMaterialManager.saveModel(repairMaterial);
            } else {//修改
                RepairMaterial repairMaterial = repairMaterialManager.get(id);
                if (repairMaterial != null) {
                    repairMaterial.setMaterialName(materialName);
                    repairMaterial.setDescription(description);
                    repairMaterial.setModifier(user.getId().intValue());
                    repairMaterial.setModifyTime(new Date());
                    repairMaterialManager.updateModel(repairMaterial);
                }
            }
            WebPageResultDto dto = new WebPageResultDto(AppConstants.SUCCESS_CODE, "成功", null);
            return dto;
        } catch (Exception e) {
            e.printStackTrace();
            return new WebPageResultDto(AppConstants.REPAIR_BILL_LIST_ERROR, "更新维修耗材异常", LogExceptionStackUtil.LogExceptionStack(e));
        }
    }


    @RequestMapping(value = "/addOrUpdateBatch")
    @ResponseBody
    public WebPageResultDto addOrUpdateBatch(@ModelAttribute("repairMaterialListDto") RepairMaterialListDto repairMaterialListDto, HttpServletRequest request) {

        LOG.info("RepairMaterialController.addOrUpdateBatch \n repairMaterialListDto:" + JsonUtils.toJson(repairMaterialListDto));

        try {
            User user = (User) request.getSession().getAttribute("UserCache");

            List<RepairMaterialDto> repairMaterialDtos = repairMaterialListDto.getRepairMaterialDtoList();
            Integer orgId = repairMaterialListDto.getOrgId();
            if (orgId == null) {
                return new WebPageResultDto(AppConstants.REPAIR_BILL_LIST_ERROR, "项目id不为空", null);
            }

            if (null != repairMaterialDtos && repairMaterialDtos.size() > 0) {
                for (RepairMaterialDto dto :
                        repairMaterialDtos) {
                    if (dto.getId() == null) { //新增
                        RepairMaterial repairMaterial = new RepairMaterial();
                        repairMaterial.setEnabled(1);
                        repairMaterial.setOrgId(orgId);
                        repairMaterial.setMaterialName(dto.getMaterialName());
                        if (StringUtil.isNotEmpty(dto.getDescription())) {
                            repairMaterial.setDescription(dto.getDescription());
                        }
                        repairMaterial.setCreateTime(new Date());
                        repairMaterial.setCreatorId(user.getId().intValue());
                        repairMaterialManager.saveModel(repairMaterial);
                    } else {//修改
                        RepairMaterial repairMaterial = repairMaterialManager.get(dto.getId());
                        if (repairMaterial != null) {
                            repairMaterial.setMaterialName(dto.getMaterialName());
                            repairMaterial.setDescription(dto.getDescription());
                            repairMaterial.setModifier(user.getId().intValue());
                            repairMaterial.setModifyTime(new Date());
                            if (dto.getEnabled() != null) {
                                repairMaterial.setEnabled(dto.getEnabled());
                            }

                            repairMaterialManager.updateModel(repairMaterial);
                        }
                    }
                }
            }

            WebPageResultDto dto = new WebPageResultDto(AppConstants.SUCCESS_CODE, "成功", null);
            return dto;
        } catch (Exception e) {
            e.printStackTrace();
            return new WebPageResultDto(AppConstants.REPAIR_BILL_LIST_ERROR, "更新地址失败", LogExceptionStackUtil.LogExceptionStack(e));
        }
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public WebPageResultDto delete(
            @RequestParam(value = "id") Long id, HttpServletRequest request) {
        LOG.info("RepairMaterialController.delete \n id:{" + id + "}");
        try {
            User user = (User) request.getSession().getAttribute("UserCache");
            RepairMaterial repairMaterial = repairMaterialManager.get(id);
            if (repairMaterial != null) {
                repairMaterial.setEnabled(0);
                repairMaterial.setModifyTime(new Date());
                repairMaterial.setModifier(user.getId().intValue());
                repairMaterialManager.updateModel(repairMaterial);
            } else {
                return new WebPageResultDto(AppConstants.REPAIR_BILL_LIST_ERROR, "未查询到维修耗材失败", null);
            }
            WebPageResultDto dto = new WebPageResultDto(AppConstants.SUCCESS_CODE, "成功", null);
            return dto;
        } catch (Exception e) {
            e.printStackTrace();
            return new WebPageResultDto(AppConstants.REPAIR_BILL_LIST_ERROR, "删除失败", LogExceptionStackUtil.LogExceptionStack(e));
        }
    }


    /**
     * 获取耗材列表
     *
     * @param orgId
     * @param request
     * @return
     */
    @RequestMapping(value = "/getMaterialList")
    @ResponseBody
    public WebPageResultDto getMaterialList(
            @RequestParam(value = "orgId") Integer orgId, HttpServletRequest request) {
        try {
//            User user = (User) request.getSession().getAttribute("UserCache");
            List<RepairMaterial> repairMaterialList = repairMaterialManager.getMaterialListByOrgId(orgId);

            WebPageResultDto dto = new WebPageResultDto(AppConstants.SUCCESS_CODE, "成功", repairMaterialList);
            return dto;
        } catch (Exception e) {
            e.printStackTrace();
            return new WebPageResultDto(AppConstants.REPAIR_BILL_LIST_ERROR, "查询列表失败", LogExceptionStackUtil.LogExceptionStack(e));
        }
    }

    @RequestMapping(value = "/getRepairUrl")
    @ResponseBody
    public WebPageResultDto getRepairUrl(@ModelAttribute("repairQrInputDto") RepairQrInputDto repairQrInputDto, @RequestParam("")  HttpServletRequest request) {
        try {
            User user = (User) request.getSession().getAttribute("UserCache");

           if(null == repairQrInputDto.getOrgId()){
               return new WebPageResultDto(AppConstants.REPAIR_BILL_LIST_ERROR, "项目id不能为空", null);
           }
          String url  = repairMaterialManager.getRepairUrl(repairQrInputDto);
            WebPageResultDto dto = new WebPageResultDto(AppConstants.SUCCESS_CODE, "成功", url);
            return dto;
        } catch (Exception e) {
            e.printStackTrace();
            return new WebPageResultDto(AppConstants.REPAIR_BILL_LIST_ERROR, "查询列表失败", LogExceptionStackUtil.LogExceptionStack(e));
        }
    }
}
