package com.jeequan.jeepay.mch.ctrl.division;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jeequan.jeepay.core.aop.MethodLog;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.db.entity.DivisionSubject;
import com.jeequan.jeepay.mch.ctrl.CommonCtrl;
import com.jeequan.jeepay.mch.service.CommonApiService;
import com.jeequan.jeepay.service.impl.DivisionSubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

/**
 * 分账对象配置表
 */
@RestController
@RequestMapping("/api/divisionSubject")
public class DivisionSubjectController extends CommonCtrl {

    @Autowired
    private DivisionSubjectService divisionSubjectService;
    @Autowired
    private CommonApiService commonApiService;

    /**
     * 接收方分页查询
     * @return
     */
    @GetMapping("recPage")
    public ApiRes recPage() {
        DivisionSubject queryObject = getObject(DivisionSubject.class).setMchNo(getCurrentMchNo()).setType(DivisionSubject.RECEIVE_TYPE);
        IPage<DivisionSubject> pages = divisionSubjectService.pageList(getIPage(true), queryObject);
        return ApiRes.page(pages);
    }

    /**
     * 分账方分页查询
     * @return
     */
    @GetMapping("divPage")
    public ApiRes divPage() {
        DivisionSubject queryObject = getObject(DivisionSubject.class).setMchNo(getCurrentMchNo()).setType(DivisionSubject.EXPEND_TYPE);
        IPage<DivisionSubject> pages = divisionSubjectService.pageList(getIPage(true), queryObject);
        return ApiRes.page(pages);
    }

    /**
     * 新增分账方
     * @return
     */
    @PreAuthorize("hasAuthority( 'ENT_DIVISION_SUBJECT_ADD' )")
    @RequestMapping(value="addDivision", method = RequestMethod.POST)
    @MethodLog(remark = "新增分账方")
    public ApiRes addDivision() {
        DivisionSubject subject = getObject(DivisionSubject.class).setType(DivisionSubject.EXPEND_TYPE).setMchNo(getCurrentMchNo());
        divisionSubjectService.addDivisionSave(subject);
        return ApiRes.ok();
    }

    /**
     * 新增分账接收方
     * @return
     */
//    @PreAuthorize("hasAuthority( 'ENT_DIVISION_SUBJECT_ADD' )")
    @RequestMapping(value="addReceiver", method = RequestMethod.POST)
    @MethodLog(remark = "新增收款方")
    public ApiRes addReceiver() {
        DivisionSubject subject = getObject(DivisionSubject.class).setType(DivisionSubject.RECEIVE_TYPE).setMchNo(getCurrentMchNo());
        commonApiService.addReceiver(subject);
        return ApiRes.ok();
    }

    @PreAuthorize("hasAuthority( 'ENT_DIVISION_SUBJECT_EDIT' )")
    @RequestMapping(value="/{divisionNo}", method = RequestMethod.PUT)
    @MethodLog(remark = "修改分账对象")
    public ApiRes update(@PathVariable("divisionNo") String divisionNo) {
        DivisionSubject update = getObject(DivisionSubject.class).setDivisionNo(divisionNo);
        DivisionSubject ext = divisionSubjectService.getById(divisionNo);
        Assert.notNull(ext,"数据非法");
        if(DivisionSubject.EXPEND_TYPE == ext.getType()){
            ext.preDivCheck();
        }else{
            ext.preRecCheck();
        }
        divisionSubjectService.updateById(update);
        return ApiRes.ok();
    }

    @PreAuthorize("hasAuthority('ENT_DIVISION_SUBJECT_DELETE')")
    @RequestMapping(value="/{divisionNo}", method = RequestMethod.DELETE)
    @MethodLog(remark = "删除分账对象")
    public ApiRes del(@PathVariable("divisionNo") String divisionNo) {
        divisionSubjectService.removeById(divisionNo);
        return ApiRes.ok();
    }
}
