package com.chy.borrow.controller;


import com.alibaba.fastjson.JSONObject;
import com.chy.borrow.entity.TmpCode;
import com.chy.borrow.eum.TmpCodeStatusEnum;
import com.chy.borrow.mapper.TmpCodeMapper;
import com.chy.borrow.service.TmpCodeService;
import com.chy.borrow.util.FileUploadUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shanghaigeography.Util.DateUtils;
import com.shanghaigeography.Util.SnowflakeConfig;
import com.shanghaigeography.entity.ResultMsg;
import com.shanghaigeography.eum.ResultState;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import com.chy.borrow.controller.BaseController;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 临时编号 前端控制器
 * </p>
 *
 * @author 沈晔浩
 * @since 2022-03-10
 */
@Controller
@CrossOrigin
@RequestMapping("/tmpCode")
public class TmpCodeController extends BaseController {

    @Autowired
    TmpCodeService tmpCodeService;

    @Autowired
    TmpCodeMapper tmpCodeMapper;

    @RequestMapping("/list")
    @ApiOperation("获取临时编号")
    @ResponseBody
    public String list(@RequestBody Map map) {
        return tmpCodeService.getList(map);
    }

    @RequestMapping("/insert")
    @ResponseBody
    public String insert(@RequestBody TmpCode tmpCode, @RequestHeader String userId) {
        try {
//            if (imgFile!=null){
//                String file = "http://47.101.207.123:8080/file/"+ FileUploadUtil.uploadToBasePath(imgFile,"D:\\file\\").replaceAll("\\\\", "/");
//                tmpCode.setImgsUrl(file);
//            }
            tmpCode.setState(TmpCodeStatusEnum.STATUS_CHECKING.getStatusCodeString());
            tmpCode.setTmpCode(getTmpCode());
            tmpCode.setApplyTime(new Date());
            tmpCode.setApplyUserId(userId);
            tmpCodeService.save(tmpCode);
            return new ResultMsg<String>().setCode(ResultState.SUCCESS).setMsg("插入成功").toJSON();
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultMsg<String>().setCode(ResultState.ERROR).setMsg("添加失败").toJSON();
        }
    }

    @RequestMapping("/check")
    @ResponseBody
    public String check(@RequestBody TmpCode tmpCode, @RequestHeader("userId") String userId) {
        try {
            boolean success = tmpCodeService.updateById(tmpCode);
            if (success) {
                return new ResultMsg<String>().setCode(ResultState.SUCCESS).setMsg("更新成功").toJSON();
            } else {
                return new ResultMsg<String>().setCode(ResultState.ERROR).setMsg("更新失败").toJSON();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultMsg<String>().setCode(ResultState.ERROR).setMsg("更新失败").toJSON();
        }
    }

    @RequestMapping("/mount")
    @ResponseBody
    public String mount(@RequestBody TmpCode tmpCode, @RequestHeader("userId") String userId) {
        try {
            boolean success = tmpCodeService.updateById(tmpCode);
            if (success) {
                return new ResultMsg<String>().setCode(ResultState.SUCCESS).setMsg("更新成功").toJSON();
            } else {
                return new ResultMsg<String>().setCode(ResultState.ERROR).setMsg("更新失败").toJSON();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultMsg<String>().setCode(ResultState.ERROR).setMsg("更新失败").toJSON();
        }
    }

    @RequestMapping("/getInfoById")
    @ResponseBody
    public String getInfoById(@RequestBody TmpCode tmpCode) {
            return tmpCodeService.getInfoById(tmpCode);
    }

    public String getTmpCode(){
        String tmpCode_before = "ls"+ DateUtils.getNowTimeStringToDD("yyyyMMdd");
        int count = tmpCodeMapper.getCountByCode(tmpCode_before);
        if (count<9){
            return tmpCode_before+"000"+(count+1);
        }else if (count<99){
            return tmpCode_before+"00"+(count+1);
        }else if(count<999){
            return tmpCode_before+"0"+(count+1);
        }else {
            return tmpCode_before+(count+1);
        }
    }
}
