package com.hzs.member.self.controller.manage;


import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
import com.hzs.common.core.enums.EApprovalBusiness;
import com.hzs.common.core.enums.EOperationBusiness;
import com.hzs.common.core.enums.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.web.controller.BaseController;

import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.member.self.CuSelfServiceLogExt;
import com.hzs.common.security.service.UserTokenService;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.util.TransactionUtils;
import com.hzs.member.self.service.ICuSelfServiceLogService;
import com.hzs.member.self.vo.DomicileChangeVO;
import com.hzs.system.sys.dto.LoginUser;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 变理户籍
 *
 * @Description:
 * @Author: ljc
 * @Time: 2023/4/18 10:07
 * @Classname: ApiDomicileChangeController
 * @Package_name: com.hzs.member.self.controller.api
 */
@Slf4j
@RequestMapping("/manage/change-domicile")
@RestController
public class DomicileChangeController extends BaseController {

    @Autowired
    private ICuSelfServiceLogService serviceLogService;

    @Autowired
    private UserTokenService userTokenService;


    /**
     * 户籍变更列表
     *
     * @return AjaxResult
     */
    @PostMapping("/domicile-change-list")
    @Log(module = EOperationModule.DOMICILE_CHANGE, business = EOperationBusiness.DOMICILE_CHANGE, method = EOperationMethod.SELECT)
    public TableDataInfo domicileChangeList(@RequestBody DomicileChangeVO domicileChangeVO) {
        domicileChangeVO.setEditType(EApprovalBusiness.DOMICILE_CHANGE.getValue());
        domicileChangeVO.setPkCountry(SecurityUtils.getPkCountry());
        startPage();
        Object[] objects = serviceLogService.selectSelfServiceByPkMember(domicileChangeVO);
        TableDataInfo tableDataInfo = getDataTable((List<CuSelfServiceLogExt>) objects[0]);
        tableDataInfo.setRows((List<DomicileChangeVO>) objects[1]);
        return tableDataInfo;
    }


    /**
     * 户籍变更列表
     *
     * @return AjaxResult
     */
    @GetMapping("/domicile-change-detail")
    @Log(module = EOperationModule.DOMICILE_CHANGE, business = EOperationBusiness.DOMICILE_CHANGE, method = EOperationMethod.SELECT, remark = "查看详情")
    public AjaxResult domicileChangeDetail(Long pkId) {
        return AjaxResult.success(serviceLogService.selectDomicileChangeDetail(pkId));
    }


    /**
     * 驳回
     *
     * @return AjaxResult
     */
    @PostMapping("/domicile-change-reject")
    @Log(module = EOperationModule.DOMICILE_CHANGE, business = EOperationBusiness.DOMICILE_CHANGE, method = EOperationMethod.REJECT)
    public AjaxResult domicileChangeReject(@RequestBody DomicileChangeVO domicileChangeVO) {
        if (StringUtils.isEmpty(domicileChangeVO.getRejectMsg())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.REJECT_MSG_NOT_NULL));
        }
        return AjaxResult.success(serviceLogService.domicileChangeRejectById(domicileChangeVO, SecurityUtils.getUserId()));
    }


    /**
     * 同意
     *
     * @return AjaxResult
     */
    @PostMapping("/domicile-change-agree")
    @Log(module = EOperationModule.DOMICILE_CHANGE, business = EOperationBusiness.DOMICILE_CHANGE, method = EOperationMethod.AGREE)
    public AjaxResult domicileChangeAgree(@RequestBody DomicileChangeVO domicileChangeVO) {
        LoginUser loginUser = userTokenService.getLoginUser();
        domicileChangeVO.setLoginUser(loginUser);
        domicileChangeVO.setPkModified(SecurityUtils.getUserId());
        domicileChangeVO.setPkCountry(SecurityUtils.getPkCountry());
        return AjaxResult.success(serviceLogService.domicileChangeAgree(domicileChangeVO));
    }


}
