package com.nantian.cloud.dbcapp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nantian.cloud.dbcapp.entity.EnterpriseState;
import com.nantian.cloud.dbcapp.service.EnterpriseStateService;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.security.service.ScUser;
import com.nantian.sc.cloud.common.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;


/**
* @Description: 企业状态

*/
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/enterpriseState")
@Api(value = "enterpriseState", tags = "企业状态")

public class EnterpriseStateController {

    private final EnterpriseStateService stateService;

    @PostMapping("/save")
    public R saveEnterpriseState(@RequestBody EnterpriseState enterpriseState){
        ScUser u= SecurityUtils.getUser();
        if(u.getId()!=null){
            enterpriseState.setUserid(u.getId());
        }
        return R.ok(stateService.save(enterpriseState));
    }

    @GetMapping("/getData")
    public R getData(String epc,String type){
        QueryWrapper<EnterpriseState> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("epc", epc);
        queryWrapper.eq("type", type);
        List<EnterpriseState> list = stateService.list(queryWrapper);
        int i = getQuarterOfYear();
        AtomicBoolean state = new AtomicBoolean(true);
        if (list.size()>0){
            list.forEach(r->{
                int month = Integer.parseInt(r.getStartTime().substring(6,2));
                int num = month/3;
                if (num==i){
                    state.set(false);
                }

            });
        }

        return R.ok(state);
    }

    private static int getQuarterOfYear() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        return calendar.get(Calendar.MONTH) / 3 + 1;
    }


}
