package com.mortals.xhx.module.baseset.web;

import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.module.baseset.model.BasesetEntity;
import com.mortals.xhx.module.baseset.model.BasesetQuery;
import com.mortals.xhx.module.baseset.service.BasesetService;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 填单服务基础设置
 *
 * @author zxfei
 * @date 2022-12-08
 */
@RestController
@RequestMapping("baseset")
public class BasesetController extends BaseCRUDJsonBodyMappingController<BasesetService, BasesetEntity, Long> {

    public BasesetController() {
        super.setModuleDesc("填单服务基础设置");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        this.addDict(model, "newsSource", new HashMap<>());
        super.init(model, context);
    }


    @Override
    protected void saveBefore(BasesetEntity entity, Map<String, Object> model, Context context) throws AppException {

        BasesetEntity basesetEntity = this.service.selectOne(new BasesetQuery().siteId(entity.getSiteId()));
        if(!ObjectUtils.isEmpty(basesetEntity)){
            entity.setId(basesetEntity.getId());
            entity.setUpdateTime(new Date());
        }


        super.saveBefore(entity, model, context);
    }
}