package com.zjrs.ggfw.portal.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.mohrss.leaf.core.framework.persistens.PageResult;
import org.mohrss.leaf.core.framework.web.controller.AjaxResponse;
import com.zjrs.ggfw.portal.bpo.UserBPO;
import com.zjrs.ggfw.portal.dto.*;
import com.zjrs.ggfw.portal.dto.common.PageDTO;
import com.zjrs.ggfw.portal.po.VRsWbSjdz;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Api("用户")
@RestController
@RequestMapping("/api/portal/user")
public class UserController {
    /**
     * 日志
     */
    private static final Logger LOG = LoggerFactory.getLogger(UserController.class);
    
    @Resource
    private UserBPO userBPO;

    @ApiOperation(value = "查询当前用户收货地址", notes = "查询当前用户收货地址List")
    @PostMapping("/querySjdzList")
    public AjaxResponse querySjdzList(@RequestBody @Validated SjdzQueryDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
    	if(bindingResult.hasErrors()) {
    		ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
    	}
        try {
            PageResult pageResult = userBPO.getSjdzList(dto);
            ajaxResponse.setData("data", pageResult);
            ajaxResponse.setData("pagesize", dto.getPagesize());
            ajaxResponse.setData("pagenum", dto.getPagenum());
        } catch (Exception e) {
            LOG.error(e.toString());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    @ApiOperation(value = "获取收货地址详细", notes = "获取收货地址详细")
    @PostMapping("/querySjdzDetail")
    public AjaxResponse querySjdzDetail(@RequestBody @Validated SjdzDetailQueryDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
    	if(bindingResult.hasErrors()) {
    		ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
    	}
        try {
        	VRsWbSjdz vRsWbSjdz = userBPO.getSjdzDetail(dto);
            ajaxResponse.setData("data", vRsWbSjdz);
        } catch (Exception e) {
            LOG.error(e.toString());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    @ApiOperation(value = "保存收货地址", notes = "保存收货地址")
    @PostMapping("/saveSjdz")
    public AjaxResponse saveSjdz(@RequestBody @Validated SjdzDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
    	if(bindingResult.hasErrors()) {
    		ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
    	}
        try {
        	//保存收件地址
        	ajaxResponse = userBPO.saveSjdz(dto);
        } catch (Exception e) {
            LOG.error(e.toString());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }
    
    @ApiOperation(value = "设置默认收件地址", notes = "设置默认收件地址")
    @PostMapping("/setMrSjdz")
    public AjaxResponse setMrSjdz(@RequestBody @Validated SjdzDetailQueryDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
    	if(bindingResult.hasErrors()) {
    		ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
    	}
        try {
        	userBPO.setMrSjdz(dto);
        } catch (Exception e) {
            LOG.error(e.toString());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }
    
    @ApiOperation(value = "删除收件地址", notes = "删除收件地址")
    @PostMapping("/deleteSjdz")
    public AjaxResponse deleteSjdz(@RequestBody @Validated SjdzDetailQueryDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
    	if(bindingResult.hasErrors()) {
    		ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
    	}
        try {
        	userBPO.deleteSjdz(dto);
        } catch (Exception e) {
            LOG.error(e.toString());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }
    
    @ApiOperation(value = "获取消息未读数量", notes = "获取消息未读数量")
    @GetMapping("/getNewsCnt")
    public AjaxResponse getNewsCnt() {
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
        	int cnt = userBPO.getNewsCnt();
        	ajaxResponse.setData("cnt", cnt);
        } catch (Exception e) {
            LOG.error(e.toString());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }
    
    @ApiOperation(value = "获取消息列表", notes = "获取消息列表")
    @PostMapping("/getNewsList")
    public AjaxResponse getNewsList(@RequestBody @Validated SjxxQueryDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
    	if(bindingResult.hasErrors()) {
    		ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
    	}
        try {
            PageResult pageResult = userBPO.getNewsList(dto);
            ajaxResponse.setData("data", pageResult);
            ajaxResponse.setData("pagesize", dto.getPagesize());
            ajaxResponse.setData("pagenum", dto.getPagenum());
        } catch (Exception e) {
            LOG.error(e.toString());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }
    
    @ApiOperation(value = "新增消息", notes = "新增消息")
    @PostMapping("/saveNews")
    public AjaxResponse saveNews(@RequestBody @Validated XxglDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
    	if(bindingResult.hasErrors()) {
    		ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
    	}
        try {
        	userBPO.saveNews(dto);
        } catch (Exception e) {
            LOG.error(e.toString());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }
    
    @ApiOperation(value = "更新消息阅读状态", notes = "更新消息阅读状态")
    @PostMapping("/setNewsYdzt")
    public AjaxResponse setNewsYdzt(@RequestBody @Validated SjxxDTO dto, BindingResult bindingResult) {
    	AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
    	if(bindingResult.hasErrors()) {
    		ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
    	}
        try {
        	ajaxResponse = userBPO.setNewsYdzt(dto);
        } catch (Exception e) {
            LOG.error(e.toString());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;	
    }
    
    @ApiOperation(value = "更新消息有效状态", notes = "更新消息有效状态")
    @PostMapping("/setNewsYxbz")
    public AjaxResponse setNewsYxbz(@RequestBody @Validated SjxxDTO dto, BindingResult bindingResult) {
    	AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
    	if(bindingResult.hasErrors()) {
    		ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
    	}
        try {
        	ajaxResponse = userBPO.setNewsYxbz(dto);
        } catch (Exception e) {
            LOG.error(e.toString());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;	
    }
    
    @ApiOperation(value = "清空回收站消息", notes = "清空回收站消息")
    @PostMapping("/setNewsClear")
    public AjaxResponse setNewsClear() {
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
        	userBPO.setNewsClear();
        } catch (Exception e) {
            LOG.error(e.toString());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }
    
    @ApiOperation(value = "获取用户信息", notes = "获取用户信息")
    @GetMapping("/getUserInfo")
    public AjaxResponse getUserInfo(GetUserInfoDTO dto) {
        LOG.info("getUserInfo...usertype="+dto.getUsertype());
    	AjaxResponse ajaxResponse = new AjaxResponse();
    	try {
        	ajaxResponse = userBPO.getUserInfo(dto.getUsertype());
    	} catch (Exception e) {
            LOG.error(e.toString());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;	
    }
    
    @ApiOperation(value = "反馈意见", notes = "反馈意见")
    @PostMapping("/saveYjfk")
    public AjaxResponse saveYjfk(@RequestBody @Validated YjfkDTO dto, BindingResult bindingResult) {
    	AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
    	if(bindingResult.hasErrors()) {
    		ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
    	}
        try {
        	ajaxResponse = userBPO.saveRsWbYjfk(dto);
        } catch (Exception e) {
            LOG.error(e.toString());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;	
    }
    
    @ApiOperation(value = "保存自助终端访问日志", notes = "保存自助终端访问日志")
    @PostMapping("/saveRsZzzdLog")
    public AjaxResponse saveRsZzzdLog(@RequestBody @Validated ZzzdLogDTO dto, BindingResult bindingResult) {
    	AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
    	if(bindingResult.hasErrors()) {
    		ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
    	}
        try {
        	ajaxResponse = userBPO.saveRsZzzdLog(dto);
        } catch (Exception e) {
            LOG.error(e.toString());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;	
    }
    
    @ApiOperation(value = "查询意见反馈列表", notes = "查询意见反馈列表")
    @PostMapping("/getRsWbYjfkList")
    public AjaxResponse getRsWbYjfkList(@RequestBody @Validated PageDTO dto, BindingResult bindingResult) {
    	AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
    	if(bindingResult.hasErrors()) {
    		ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
    	}
        try {
        	ajaxResponse = userBPO.getRsWbYjfkList(dto);
        } catch (Exception e) {
            LOG.error(e.toString());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;	
    }
    
    @ApiOperation(value = "查询意见反馈详情", notes = "查询意见反馈详情")
    @PostMapping("/getRsWbYjfk")
    public AjaxResponse getRsWbYjfk(@RequestBody @Validated YjfkQueryDTO dto, BindingResult bindingResult) {
    	AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
    	if(bindingResult.hasErrors()) {
    		ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
    	}
        try {
        	ajaxResponse = userBPO.getRsWbYjfk(dto);
        } catch (Exception e) {
            LOG.error(e.toString());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;	
    }
    @ApiOperation(value = "验证身份,确定", notes = "验证身份,确定")
    @PostMapping("/checkIdentity")
    public AjaxResponse checkIdentity(@RequestBody @Validated CheckMsgCodeDTO dto) {
        LOG.info("checkIdentity,into...");
        return userBPO.checkIdentity(dto);
    }

    /**
     * 获取图片验证码
     */
    @ApiOperation(value = "验证身份,获取图片验证码", notes = "验证身份,获取图片验证码")
    @GetMapping("/render")
    public void createKaptcha(HttpServletRequest request, HttpServletResponse response) {
        LOG.info("render...");
        userBPO.render();
    }

    @ApiOperation(value = "获取手机短信验证码", notes = "获取手机短信验证码")
    @PostMapping("/sendPhoneMsgCode")
    public AjaxResponse sendPhoneMsgCode(@RequestBody @Validated CheckImgCodeDTO dto) {
        LOG.info("sendPhoneMsgCode...");
        return userBPO.sendPhoneMsgCode(dto);
    }

}
