package com.palmnest.webapp.controller.backstage;

import com.alibaba.fastjson.JSON;
import com.palmnest.application.AppConstants;
import com.palmnest.application.core.util.LogExceptionStackUtil;
import com.palmnest.application.dto.WebPageResultDto;
import com.palmnest.application.dto.input.RepairAddressDto;
import com.palmnest.application.dto.input.RepairAddressListDto;
import com.palmnest.model.User;
import com.palmnest.model.repair.RepairAddress;
import com.palmnest.service.repair.RepairAddressManager;
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.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

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

/**
 * 服务之窗1L- RepairAddressController
 *
 * @Description TODO
 * @author: jiangyong
 * @since: 2020/8/13 16:46
 */
@Controller
@RequestMapping(value = "/repairAddress")
public class RepairAddressController extends BaseFormController {
    private Logger LOG = Logger.getLogger(RepairAddressController.class);
    @Autowired
    private RepairAddressManager repairAddressManager;

    /**
     * PC 更新维修地址
     *
     * @param id
     * @param orgId
     * @param address
     * @param orderNo
     * @param request
     * @return
     */
    @RequestMapping(value = "/addOrUpdate")
    @ResponseBody
    public WebPageResultDto addOrUpdate(
            @RequestParam(value = "id", required = false) Long id, @RequestParam(value = "orgId") Integer orgId,
            @RequestParam(value = "address") String address,
            @RequestParam(value = "orderNo", required = false) Integer orderNo, HttpServletRequest request) {

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

        try {
            User user = (User) request.getSession().getAttribute("UserCache");
            if (id == null) { //新增
                RepairAddress repairAddress = new RepairAddress();
                repairAddress.setEnabled(1);
                repairAddress.setOrgId(orgId);
                repairAddress.setAddress(address);
                if (null != orderNo) {
                    repairAddress.setOrderNo(orderNo);
                }
                repairAddress.setCreateTime(new Date());
                repairAddress.setCreatorId(user.getId().intValue());
                repairAddressManager.saveModel(repairAddress);
            } else {//修改
                RepairAddress repairAddress = repairAddressManager.get(id);
                if (repairAddress != null) {
                    repairAddress.setAddress(address);
                    repairAddress.setOrderNo(orderNo);
                    repairAddressManager.updateModel(repairAddress);
                }
            }
            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("repairAddressListDto") RepairAddressListDto repairAddressListDto, HttpServletRequest request) {

        LOG.info("RepairMaterialController.addOrUpdateBatch \n repairAddressListDto:" + JSON.toJSON(repairAddressListDto));

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

            List<RepairAddressDto> repairAddressDtos = repairAddressListDto.getAddressList();
            Integer orgId = repairAddressListDto.getOrgId();
            if (orgId == null) {
                return new WebPageResultDto(AppConstants.REPAIR_BILL_LIST_ERROR, "项目id不为空", null);
            }

            if (null != repairAddressDtos && repairAddressDtos.size() > 0) {
                for (RepairAddressDto dto :
                        repairAddressDtos) {
                    if (dto.getId() == null) { //新增
                        RepairAddress repairAddress = new RepairAddress();
                        repairAddress.setEnabled(1);
                        repairAddress.setOrgId(orgId);
                        repairAddress.setAddress(dto.getAddress());
                        if (null != dto.getOrderNo()) {
                            repairAddress.setOrderNo(dto.getOrderNo());
                        }
                        repairAddress.setCreateTime(new Date());
                        repairAddress.setCreatorId(user.getId().intValue());
                        repairAddressManager.saveModel(repairAddress);
                    } else {//修改
                        RepairAddress repairAddress = repairAddressManager.get(dto.getId());
                        if (repairAddress != null) {
                            repairAddress.setAddress(dto.getAddress());
                            repairAddress.setOrderNo(dto.getOrderNo());
                            if (dto.getEnabled() != null) {
                                repairAddress.setEnabled(dto.getEnabled());
                            }
                            repairAddressManager.updateModel(repairAddress);
                        }
                    }
                }
            }

            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");
            RepairAddress repairAddress = repairAddressManager.get(id);
            if (repairAddress != null) {
                repairAddress.setEnabled(0);
                repairAddressManager.updateModel(repairAddress);
            } 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 = "/getAddressList")
    @ResponseBody
    public WebPageResultDto getAddressList(
            @RequestParam(value = "orgId") Integer orgId, HttpServletRequest request) {
        try {
//            User user = (User) request.getSession().getAttribute("UserCache");
            List<RepairAddress> repairAddresses = repairAddressManager.getListByOrgId(orgId);

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