package com.hzs.ground.supplier.controller;

import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
import com.hzs.common.core.constant.msg.ScmMsgConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.*;
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.ground.supplier.ThSupplierApply;
import com.hzs.common.domain.member.ext.CuMemberExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.util.TransactionUtils;
import com.hzs.ground.supplier.service.IThSupplierApplyService;
import com.hzs.ground.supplier.vo.ThSupplierApplyVO;
import com.hzs.member.base.IMemberServiceApi;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 供应商申请申请 前端控制器
 */
@RestController
@RequestMapping("/manage/th-supplier-apply")
public class ThSupplierApplyController extends BaseController {

    @Autowired
    private IThSupplierApplyService iThSupplierApplyService;

    @DubboReference
    IMemberServiceApi iMemberServiceApi;

    /**
     * 供应商申请列表
     **/
    @GetMapping("/list")
    @Log(module = EOperationModule.TH_SUPPLIER_APPLY, business = EOperationBusiness.TH_SUPPLIER_APPLY, method = EOperationMethod.SELECT)
    public TableDataInfo listGeneralIn(ThSupplierApplyVO thSupplierApplyVO) {
        Long userId = SecurityUtils.getUserId();
        thSupplierApplyVO.setPkCountry(SecurityUtils.getPkCountry());
        Integer checkUser = iThSupplierApplyService.queryThSupplierApplyUserList(userId);
        if (checkUser > 0) {
            thSupplierApplyVO.setPkCreator(userId);
        }
        startPage();
        List<ThSupplierApplyVO> thSupplierApplyList = iThSupplierApplyService.queryThSupplierApplyList(thSupplierApplyVO);
        return getDataTable(thSupplierApplyList);
    }

    /**
     * 添加供应商申请信息
     **/
    @PostMapping("/save")
    public AjaxResult save(@RequestBody ThSupplierApplyVO thSupplierApplyVO) {
        int pkCountry = SecurityUtils.getPkCountry();
        // 验证必填信息
        if (StringUtils.isEmpty(thSupplierApplyVO.getName())) {
            return AjaxResult.error(TransactionUtils.getContent(ScmMsgConstants.TH_SUPPLIER_APPLY_NAME));
        }
        if (StringUtils.isEmpty(thSupplierApplyVO.getShortName())) {
            return AjaxResult.error(TransactionUtils.getContent(ScmMsgConstants.TH_SUPPLIER_APPLY_SHORT_NAME));
        }
        if (StringUtils.isEmpty(thSupplierApplyVO.getLegalBody())) {
            return AjaxResult.error(TransactionUtils.getContent(ScmMsgConstants.TH_SUPPLIER_APPLY_LEGAL_BODY));
        }
        if (thSupplierApplyVO.getSettleType() == ESettleType.ONLINE.getValue()) {
            if (StringUtils.isEmpty(thSupplierApplyVO.getMemberCode())) {
                return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_CODE_NOT_NULL));
            }
            R<CuMemberExt> member = iMemberServiceApi.getMemberByCode(thSupplierApplyVO.getMemberCode());
            thSupplierApplyVO.setPkMember(member.getData().getPkId());
        }
        //默认线下
        thSupplierApplyVO.setSettleType(ESettleType.OFFLINE.getValue());
        thSupplierApplyVO.setPkCountry(pkCountry);
        thSupplierApplyVO.setPkCreator(SecurityUtils.getUserId());
        thSupplierApplyVO.setCreationTime(new Date());
        iThSupplierApplyService.saveThSupplierApply(thSupplierApplyVO);
        return AjaxResult.success();
    }

    /**
     * 修改供应商申请信息
     **/
    @PostMapping("/update")
    public AjaxResult update(@RequestBody ThSupplierApplyVO thSupplierApplyVO) {
        int pkCountry = SecurityUtils.getPkCountry();
        // 验证必填信息
        if (StringUtils.isEmpty(thSupplierApplyVO.getName())) {
            return AjaxResult.error(TransactionUtils.getContent(ScmMsgConstants.TH_SUPPLIER_APPLY_NAME));
        }
        if (StringUtils.isEmpty(thSupplierApplyVO.getShortName())) {
            return AjaxResult.error(TransactionUtils.getContent(ScmMsgConstants.TH_SUPPLIER_APPLY_SHORT_NAME));
        }
        if (StringUtils.isEmpty(thSupplierApplyVO.getLegalBody())) {
            return AjaxResult.error(TransactionUtils.getContent(ScmMsgConstants.TH_SUPPLIER_APPLY_LEGAL_BODY));
        }
        if (thSupplierApplyVO.getSettleType() == ESettleType.ONLINE.getValue()) {
            if (StringUtils.isEmpty(thSupplierApplyVO.getMemberCode())) {
                return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_CODE_NOT_NULL));
            }
            R<CuMemberExt> member = iMemberServiceApi.getMemberByCode(thSupplierApplyVO.getMemberCode());
            thSupplierApplyVO.setPkMember(member.getData().getPkId());
        }
        //默认线下
        thSupplierApplyVO.setSettleType(ESettleType.OFFLINE.getValue());
        thSupplierApplyVO.setPkCountry(pkCountry);
        thSupplierApplyVO.setPkCreator(SecurityUtils.getUserId());
        thSupplierApplyVO.setCreationTime(new Date());
        iThSupplierApplyService.updateThSupplierApply(thSupplierApplyVO);
        return AjaxResult.success();
    }

    /**
     * 查询供应商申请信息
     **/
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Long pkId) {
        ThSupplierApply tsa = iThSupplierApplyService.getById(pkId);
        return AjaxResult.success(tsa);
    }

    /**
     * 删除供应商申请信息
     **/
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Long pkId) {
        return AjaxResult.success(iThSupplierApplyService.delThSupplierApply(pkId));
    }


}
