package easydo.technology.controller;

import easydo.technology.components.JdbcClient;
import easydo.technology.enums.MESEnum;
import easydo.technology.exception.BizException;
import easydo.technology.model.QualityInspectProgram;
import easydo.technology.service.FlowNoService;
import easydo.technology.system.model.SysUser;
import easydo.technology.utils.StringUtil;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/qualityInspectProgram")
public class QualityInspectProgramController {
    @Resource
    JdbcClient jdbcClient;
    @Resource
    DataSource dataSource;
    @Resource
    FlowNoService flowNoService;

    @RequestMapping(value = "/getList")
    public Object getList(@RequestBody Map<String, Object> map) throws Exception {
        List<QualityInspectProgram> list = jdbcClient.getJdbcList(map, QualityInspectProgram.class);
        return new ResponseEntity<>(list, HttpStatus.OK);
    }

    @RequestMapping(value = "/getPage")
    public Object getPage(@RequestBody Map<String, Object> map) throws Exception {
        Connection connection = dataSource.getConnection();
        try {
            Map<String, Object> recordsPage = jdbcClient.getJdbcPage(map, QualityInspectProgram.class, connection);
            List<QualityInspectProgram> records = (List<QualityInspectProgram>) recordsPage.get("records");
            for (QualityInspectProgram model : records) {
                jdbcClient.getMinioFile(model, connection);

                SysUser inspectUser = new SysUser();
                inspectUser.setId(model.getInspectUserId());
                inspectUser = jdbcClient.getJdbcModelById(inspectUser,connection);
                model.setInspectUserName(inspectUser.getNickName());

                SysUser reviewUser = new SysUser();
                reviewUser.setId(model.getReviewUserId());
                reviewUser = jdbcClient.getJdbcModelById(reviewUser, connection);
                model.setReviewUserName(reviewUser.getNickName());
            }
            return new ResponseEntity<>(recordsPage, HttpStatus.OK);
        } catch (Exception e) {
            throw new BizException(e.getMessage());
        } finally {
            connection.close();
        }
    }


    @RequestMapping(value = "/save")
    public Object add(@RequestBody QualityInspectProgram model) throws Exception {
        Connection connection = dataSource.getConnection();
        try {
            connection.setAutoCommit(false);
            generateCode(model, connection);
            jdbcClient.jdbcInsert(model, connection);
            connection.commit();
            return new ResponseEntity<>(model, HttpStatus.OK);
        } catch (Exception e) {
            connection.rollback();
            throw new BizException(e.getMessage());
        } finally {
            connection.close();
        }
    }


    @RequestMapping(value = "/update")
    public Object update(@RequestBody QualityInspectProgram model) throws Exception {
        jdbcClient.jdbcUpdateById(model);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @RequestMapping(value = "/remove")
    public Object remove(@RequestBody QualityInspectProgram model) throws Exception {
        jdbcClient.jdbcRemoveById(model);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    private synchronized void generateCode(QualityInspectProgram model, Connection connection) throws Exception {
        if (StringUtil.isEmpty(model.getCode())) {
            while (true) {
                String flowNo = flowNoService.getFlowNo(MESEnum.FLOW_NO_TYPE_QUALITY_INSPECT_PROGRAM.getValue(), connection);
                QualityInspectProgram param = new QualityInspectProgram();
                param.setCode(flowNo);
                int count = jdbcClient.getJdbcCount(param, connection);
                if (count == 0) {
                    model.setCode(flowNo);
                    break;
                }
            }
        } else {
            QualityInspectProgram param = new QualityInspectProgram();
            param.setCode(model.getCode());
            int count = jdbcClient.getJdbcCount(param, connection);
            if (count > 0) {
                throw new BizException("编号已存在，请重新填写");
            }
        }
    }
}
