package com.bweb.controller.audit;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.security.SessionObject;
import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.AuditMsg;
import com.hfkj.entity.GoodsMsg;
import com.hfkj.entity.GoodsSpecs;
import com.hfkj.entity.GoodsVpd;
import com.hfkj.model.ResponseData;
import com.hfkj.model.SecUserSessionObject;
import com.hfkj.model.audit.AuditModel;
import com.hfkj.model.goods.GoodsModel;
import com.hfkj.model.goods.GoodsModelSpecs;
import com.hfkj.service.audit.AuditMsgService;
import com.hfkj.service.goods.GoodsMsgService;
import com.hfkj.service.goods.GoodsSpecsService;
import com.hfkj.service.goods.GoodsVpdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
@RequestMapping(value="/audit")
@Api(value="审核业务")
public class AuditController {

    private static final Logger log = LoggerFactory.getLogger(AuditController.class);

    @Resource
    private AuditMsgService auditMsgService;

    @Resource
    private UserCenter userCenter;

    @Resource
    private GoodsMsgService goodsMsgService;

    @Resource
    private GoodsSpecsService goodsSpecsService;

    @Resource
    private GoodsVpdService goodsVpdService;

    @RequestMapping(value="/getListGoodsAudit",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询列表")
    public ResponseData getListGoodsAudit(@RequestParam(value = "submitObjectName" , required = false) String submitObjectName,
                                     @RequestParam(value = "submitOpName" , required = false) String submitOpName,
                                     @RequestParam(value = "status" , required = false) Integer status,
                                     @RequestParam(value = "pageNum" , required = true) Integer pageNum,
                                     @RequestParam(value = "pageSize" , required = true) Integer pageSize) {
        try {

            Map<String , Object> map = new HashMap<>();
            map.put("submitObjectName", submitObjectName);
            map.put("submitOpName", submitOpName);
            map.put("status", status);

            PageHelper.startPage(pageNum, pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(auditMsgService.getList(map)));

        } catch (Exception e) {
            log.error("error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/auditMsg",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "审核内容")
    public ResponseData auditMsg(
            @RequestBody AuditMsg body, HttpServletRequest request
    ) {
        try {

            SessionObject sessionObject = userCenter.getSessionObject(request);
            SecUserSessionObject userModel = (SecUserSessionObject) sessionObject.getObject();

            if (body == null
                    || body.getId() == null
                    || body.getStatus() == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            AuditMsg auditMsg = auditMsgService.queryDetail(body.getId());
            if (auditMsg.getStatus() != 2) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "当前状态不可操作");
            }

            auditMsg.setStatus(body.getStatus());
            auditMsg.setRemarks(body.getRemarks());
            auditMsg.setAuditOpId(userModel.getAccount().getId());
            auditMsg.setAuditOpName(userModel.getAccount().getUserName());

            auditMsgService.update(auditMsg);

            GoodsMsg goodsMsg = goodsMsgService.queryDetail(auditMsg.getSubmitObjectId());

            goodsMsg.setStatus(body.getStatus() == 0 ? 2 : 1);
            goodsMsg.setUpdateTime(new Date());

            goodsMsgService.update(goodsMsg);

            return ResponseMsgUtil.success("成功");

        } catch (Exception e) {
            log.error("error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/auditDetail",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "审核详情")
    public ResponseData auditDetail(@RequestParam(value = "id" , required = false) Long id) {
        try {

            AuditModel auditModel = new AuditModel();
            AuditMsg auditMsg = auditMsgService.queryDetail(id);

            if (auditMsg == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未查询到相关内容！");
            }

            BeanUtils.copyProperties(auditMsg, auditModel);

            GoodsMsg goodsMsg = goodsMsgService.queryDetail(auditMsg.getSubmitObjectId());

            if (goodsMsg == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未查询到相关内容！");
            }


            GoodsModel goodsModel = new GoodsModel();

            BeanUtils.copyProperties(goodsMsg, goodsModel);

            Map<String , Object> map = new HashMap<>();
            map.put("goodsId" , goodsMsg.getId());

            List<GoodsSpecs> goodsSpecs = goodsSpecsService.getList(map);

            List<GoodsModelSpecs> goodsModelSpecs = new ArrayList<>();

            for (GoodsSpecs goodsSpec : goodsSpecs) {
                GoodsModelSpecs goodsModelSpec = new GoodsModelSpecs();
                BeanUtils.copyProperties(goodsSpec, goodsModelSpec);
                GoodsVpd goodsVpd = goodsVpdService.queryDetailBySpecsId(goodsSpec.getId());
                if (goodsVpd != null) {
                    BeanUtils.copyProperties(goodsVpd, goodsModelSpec);
                }
                goodsModelSpec.setSpecsId(goodsSpec.getId());
                goodsModelSpecs.add(goodsModelSpec);
            }

            goodsModel.setGoodsModelSpecs(goodsModelSpecs);

            auditModel.setGoodsModel(goodsModel);

            return ResponseMsgUtil.success(auditModel);

        } catch (Exception e) {
            log.error("error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

}
