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

import com.mortals.framework.annotation.UnAuth;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.OrderCol;
import com.mortals.framework.service.ICacheService;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.common.key.RedisKey;
import com.mortals.xhx.module.device.model.DeviceEntity;
import com.mortals.xhx.module.device.model.DeviceQuery;
import com.mortals.xhx.module.device.service.DeviceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import com.mortals.framework.model.Context;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.module.sample.model.SampleBillEntity;
import com.mortals.xhx.module.sample.service.SampleBillService;
import org.apache.commons.lang3.ArrayUtils;
import com.mortals.framework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import static com.mortals.framework.ap.SysConstains.*;

/**
 * 样报服务
 *
 * @author zxfei
 * @date 2023-02-23
 */
@RestController
@RequestMapping("sample/bill")
@Slf4j
public class SampleBillController extends BaseCRUDJsonBodyMappingController<SampleBillService, SampleBillEntity, Long> {

    @Autowired
    private ParamService paramService;

    @Autowired
    public DeviceService deviceService;
    @Autowired
    private ICacheService cacheService;

    public SampleBillController() {
        super.setModuleDesc("样报服务");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        super.init(model, context);
    }


    @Override
    protected void doListBefore(SampleBillEntity query, Map<String, Object> model, Context context) throws AppException {
        super.doListBefore(query, model, context);
        List<OrderCol> orderColList = new ArrayList<>();
        orderColList.add(new OrderCol("operTime", OrderCol.DESCENDING));
        query.setOrderColList(orderColList);
    }

    @Override
    @UnAuth
    public String save(@RequestBody SampleBillEntity entity) {
        if (ObjectUtils.isEmpty(entity.getDeviceName()) && !ObjectUtils.isEmpty(entity.getDeviceCode())) {
            DeviceEntity deviceEntity = deviceService.selectOne(new DeviceQuery().deviceCode(entity.getDeviceCode()));
           if(!ObjectUtils.isEmpty(deviceEntity)){
               entity.setDeviceName(deviceEntity.getDeviceName());
           }

        }
        entity.setMatterFullName(entity.getMatterName());

        entity.setMaterialFullName(entity.getMaterialName());
        cacheService.incrForToday(RedisKey.KEY_DAY_VIEW);
        cacheService.incr(RedisKey.KEY_DAY_VIEW_TOTAL);
        return super.save(entity);
    }
}