package com.jpsoft.shinestar.modules.mobile.controller;


import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.base.dto.EpidemicReportDTO;
import com.jpsoft.shinestar.modules.base.entity.EpidemicReport;
import com.jpsoft.shinestar.modules.base.entity.PersonInfo;
import com.jpsoft.shinestar.modules.base.service.EpidemicReportService;
import com.jpsoft.shinestar.modules.base.service.PersonInfoService;
import com.jpsoft.shinestar.modules.base.service.PersonPopedomService;
import com.jpsoft.shinestar.modules.common.dto.MessageResult;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.jpsoft.shinestar.modules.common.utils.PojoUtils;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("/mobile/epidemicReportApi")
public class EpidemicReportApiController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private EpidemicReportService epidemicReportService;

    @Autowired
    private PersonInfoService personInfoService;

    @ApiOperation(value="疫情上报")
    @PostMapping("add")
    public MessageResult<EpidemicReport> add(@RequestBody EpidemicReportDTO dto,@RequestAttribute String subject){
        MessageResult<EpidemicReport> msgResult = new MessageResult<>();

        try {
            EpidemicReport epidemicReport=new EpidemicReport();

            epidemicReport.setId(UUID.randomUUID().toString());
            epidemicReport.setCreateBy(subject);
            epidemicReport.setCreateTime(new Date());
            epidemicReport.setDelFlag(false);
            epidemicReport.setStatus("1");//提交成功
            epidemicReport.setContent(dto.getContent());
            epidemicReport.setImg(dto.getImg());
            epidemicReport.setPersonId(Long.parseLong(subject));
            epidemicReport.setPhone(dto.getPhone());

            int affectCount = epidemicReportService.insert(epidemicReport);

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(epidemicReport);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("数据库添加失败");
            }
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }

    @ApiOperation(value="疫情上报详情")
    @GetMapping("detail/{id}")
    public MessageResult<EpidemicReportDTO> detail(@PathVariable("id") String id,String token){
        MessageResult<EpidemicReportDTO> msgResult = new MessageResult<>();

        try {
            EpidemicReport epidemicReport = epidemicReportService.get(id);

            if (epidemicReport != null) {
                EpidemicReportDTO epidemicReportDTO = new EpidemicReportDTO();

                epidemicReportDTO.setId(epidemicReport.getId());

                PersonInfo personInfo = personInfoService.get(epidemicReport.getPersonId());

                epidemicReportDTO.setName(personInfo.getName());
                epidemicReportDTO.setPhone(epidemicReport.getPhone());
                epidemicReportDTO.setContent(epidemicReport.getContent());
                epidemicReportDTO.setCreateTime(epidemicReport.getCreateTime());
                epidemicReportDTO.setImg(epidemicReport.getImg());
                epidemicReportDTO.setStatus(epidemicReport.getStatus());

                if(epidemicReportDTO.getStatus().equals("1")) {
                    epidemicReportDTO.setStatusN("提交成功");
                }
                else{
                    epidemicReportDTO.setStatusN("提交失败");
                }

                msgResult.setResult(true);
                msgResult.setData(epidemicReportDTO);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("数据库不存在该记录!");
            }
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }

    @ApiOperation(value="更新")
    @PostMapping("update")
    public MessageResult<EpidemicReport> update(@ModelAttribute EpidemicReport epidemicReport){
        MessageResult<EpidemicReport> msgResult = new MessageResult<>();

        try {
            int affectCount = epidemicReportService.update(epidemicReport);

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(epidemicReport);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("数据库更新失败");
            }
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }


    @ApiOperation(value="疫情上报记录")
    @RequestMapping(value = "pageList",method = RequestMethod.POST)
    public MessageResult<Map> pageList(
            String token,
            @RequestParam(value="pageIndex",defaultValue="1") int pageIndex,
            @RequestParam(value="pageSize",defaultValue="20") int pageSize,
            HttpServletRequest request){
        String subject = (String)request.getAttribute("subject");

        //当前用户ID
        System.out.println(subject);

        MessageResult<Map> msgResult = new MessageResult<>();

        Map<String,Object> searchParams = new HashMap<>();

        List<Sort> sortList = new ArrayList<>();
        sortList.add(new Sort("create_time","desc"));

        PersonInfo person = personInfoService.get(Long.parseLong(subject));

        searchParams.put("personId",person.getId());

        Page<EpidemicReport> page = epidemicReportService.pageSearch(searchParams,pageIndex,pageSize,sortList);

        Page<EpidemicReportDTO> pageDto = new Page<>();

        for (EpidemicReport epidemicReport:page) {
            EpidemicReportDTO epidemicReportDTO = new EpidemicReportDTO();

            epidemicReportDTO.setId(epidemicReport.getId());

            PersonInfo personInfo = personInfoService.get(epidemicReport.getPersonId());

            epidemicReportDTO.setName(personInfo.getName());
            epidemicReportDTO.setPhone(epidemicReport.getPhone());
            epidemicReportDTO.setContent(epidemicReport.getContent());
            epidemicReportDTO.setCreateTime(epidemicReport.getCreateTime());
            epidemicReportDTO.setImg(epidemicReport.getImg());
            epidemicReportDTO.setStatus(epidemicReport.getStatus());

            if(epidemicReportDTO.getStatus().equals("1")) {
                epidemicReportDTO.setStatusN("提交成功");
            }
            else{
                epidemicReportDTO.setStatusN("提交失败");
            }

            pageDto.add(epidemicReportDTO);

        }

        pageDto.setPageSize(page.getPageSize());
        pageDto.setTotal(page.getTotal());
        pageDto.setPageNum(page.getPageNum());

        msgResult.setResult(true);
        msgResult.setData(PojoUtils.pageWrapper(pageDto));

        return msgResult;
    }
}
